How to Debug Variables in Smarty like in PHP var_dump()
PhpTemplatesDebuggingSmartyPhp Problem Overview
I have some variables inside a template and I don't know where I assigned them. I need to know what is inside a particular variable; for instance, say I have a variable in smarty called member
. I tried with {debug}
but it didn't work, and no popup was shown.
How can I output/debug smarty variables using something like var_dump()
inside the templates?
Php Solutions
Solution 1 - Php
You can use {php}
tags
Method 1 (won't work in Smarty 3.1 or later):
{php}
$var =
$this->get_template_vars('var');
var_dump($var);
{/php}
Method 2:
{$var|@print_r}
Method 3:
{$var|@var_dump}
Solution 2 - Php
This should work:
{$var|@print_r}
or
{$var|@var_dump}
The @ is needed for arrays to make smarty run the modifier against the whole thing, otherwise it does it for each element.
Solution 3 - Php
For what it's worth, you can do {$varname|@debug_print_var}
to get a var_dump()
-esque output for your variable.
Solution 4 - Php
just use {debug} in your .tpl and look at your sourcecode
Solution 5 - Php
In new Smarty it is:
<pre>
{var_dump($variable)}
</pre>
Solution 6 - Php
Try out with the Smarty Session:
{$smarty.session|@debug_print_var}
or
{$smarty.session|@print_r}
To beautify your output, use it between <pre> </pre>
tags
Solution 7 - Php
try this .... Set $debugging
to TRUE in Smarty.
Solution 8 - Php
To debug in smarty in prestashop 1.6.x :
{ddd($variable)} -> debug and die
{ppp($variable)} -> debug only
An onther usefull debug tag :
{debug}
Solution 9 - Php
If you want something prettier I would advise
{"<?php\n\$data =\n"|@cat:{$yourvariable|@var_export:true|@cat:";\n?>"}|@highlight_string:true}
just replace yourvariable by your variable
Solution 10 - Php
in smarty V3 you can use this
{var_dump($variable)}
Solution 11 - Php
I prefer to use <script>console.log({$varname|@json_encode})</script>
to log to the console.
Solution 12 - Php
{$variable|@debug_print_var nofilter}
and you not need to add "<pre>
" tags
{$variable|var_dump}
show you more (but worse formatting) because debug_print_var
not showing private variable in object!
Solution 13 - Php
In smarty there is built in modifier you could use that by using |
(single pipeline operator). Like this {$varname|@print_r}
will print value as print_r($php_variable)