Display the contents of an array in Smarty

Some time you just need to see what variables are available in an array in Smarty. In PHP this is usually done using “print_r($foo)” where $foo is your array. But in smarty this is done using the following method.

{$foo|@print_r}

When you use this command I recommend placing open and closing pre tags around the command to ensure the output is formatted in a readable format.

The @ tells smarty to pass the entire array to the modifier rather than calling the modifier for each element of the array. Smarty also ships with a modifier that does similar to print_r(): {$foo|@debug_print_var}. You may also consider smarty’s debug facility (which you can enable in-template by inserting a {debug} tag or you can enable by configuring the Smarty object appropriately — see the docs for more info)

Tags: , ,

Leave a Comment