Howto generate json with smarty?
This should work. The @ makes smarty run the modifier against the whole array, otherwise it does it for each element.
{$myarray|@json_encode}
If $escape_html is enabled, you will need to use nofilter
:{$myarray|@json_encode nofilter}
How to print a json in Smarty
You have 2 possible solutions.
First solution
in PHP you use:
$data = '[
{
"first_name": "jinu",
"last_name": "mk",
"loginid": "jinu@amt.in",
"timezone": "5.5",
"team_id": "c964ef415f157ddd99173f5b481ee1e3",
"user_type": 1,
"last_login_date": null
},
{
"first_name": "avatar second",
"last_name": "test",
"loginid": "avatar2@gmail.com",
"timezone": "5.5",
"team_id": "ec40f5feda8643135bc20be44f897b03",
"user_type": "3",
"last_login_date": null
},
{
"first_name": "avatar testing admin",
"last_name": "amt 1",
"loginid": "avatar@amt.in",
"timezone": "5.5",
"team_id": "ec40f5feda8643135bc20be44f897b03",
"user_type": 1,
"last_login_date": null
}
]';
$smarty->assign('games',$data);
In Smarty you use:{foreach from=$games|json_decode item=foo}
<li>{$foo->first_name}</li>
{/foreach}
However I'm not sure in this case if json_decode
is run on $games
just once or on each invocation.Second solution
In PHP you use:
$data = '[
{
"first_name": "jinu",
"last_name": "mk",
"loginid": "jinu@amt.in",
"timezone": "5.5",
"team_id": "c964ef415f157ddd99173f5b481ee1e3",
"user_type": 1,
"last_login_date": null
},
{
"first_name": "avatar second",
"last_name": "test",
"loginid": "avatar2@gmail.com",
"timezone": "5.5",
"team_id": "ec40f5feda8643135bc20be44f897b03",
"user_type": "3",
"last_login_date": null
},
{
"first_name": "avatar testing admin",
"last_name": "amt 1",
"loginid": "avatar@amt.in",
"timezone": "5.5",
"team_id": "ec40f5feda8643135bc20be44f897b03",
"user_type": 1,
"last_login_date": null
}
]';
$smarty->assign('games',json_decode($data));
In Smarty file:{foreach from=$games item=foo}
<li>{$foo->first_name}</li>
{/foreach}
I always recommend using second method, because if possible in Smarty you should avoid using any calculations and just display data. transforming array into json - smarty
Does this help?
$myArray = array(
array(
"x" => "Sat",
"y" => "6",
),
array(
"x" => "Sun",
"y" => "10",
)
);
$json = json_encode($myArray);
print_r($json);
Output: [{"x":"Sat","y":"6"},{"x":"Sun","y":"10"}] Pretty print json object in smarty
The easiest thing to do is to convert the object to an associative array, and then parse it. The function would look like this:
function format_json($array, $indent) {
$indent_text = '';
for ($ii = 0;$ii < $indent; $ii++)
$indent_text .= ' ';
echo '<br />'.$indent_text.'{<br />';
foreach ($array as $key => $value) {
echo $indent_text.'"'.$key.'" : ';
if (is_array($value))
format_json( $value, $indent + 1 );
else echo '"'.$value.'"; <br />';
}
echo $indent_text.'}<br />';
}
<pre><code><?php format_json(json_decode($your_json_object, true), 0); ?></code></pre>
Obviously, you can style the tag and change the indentation any way you want. Is it possible to pass smarty functions into json html output?
Maybe value.comment
just needs quotes:
html += '<p>{"'+value.comment+'"|smarty_modifier_autolink|nl2br|mention}</p>';
Have a look at this: http://www.smarty.net/docs/en/language.modifiers.tplNormally after {
there should be a smarty plugin, php variable or smarty command. If you want to add a modifier to a content string then you have to surround it by quotes.
How to parse/decode JSON object in smarty template?
JSON string is just string. To access its members you have to create array/object from this string:
{foreach from=$items item=entry}
{* create array from JSON string*}
{assign var=person value=$entry->nb_persons|json_decode:1}
<pre>
{$person.company}
</pre>
{/foreach}
How to insert json string into smarty template?
My issue was that smarty converted quotes and other stuff to html entities. The answer is to add nofilter parameter to the template variable
var groups = {$groups nofilter};
Related Topics
PHP Upload File Enhance Security
What Is the Js Equivalent to the PHP Function Number_Format
Make Script Execution to Unlimited
Php/MySQL with Encoding Problems
Typecasting VS Function to Convert Variable Type in PHP
PHP MySQL - Insert into Without Using Column Names But with Autoincrement Field
Can One Modify the Templates Created by Artisan Migrate Command
File_Get_Contents('Php://Input') Always Returns an Empty String
MySQL Insert ....On Duplicate Update - Adds One to the Autoincrement
How to Insert JSON Array into MySQL Database
Make Woocommerce Checkout Phone Field Optional Based on Shipping Country
Cannot Set Date.Timezone in PHP.Ini File
PHP - Simplexml - Addchild with Another Simplexmlelement
How to Use Xpath and Dom to Replace a Node/Element in PHP