How to Debug Variables in Smarty like in PHP var_dump()

285,992

Solution 1

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

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

For what it's worth, you can do {$varname|@debug_print_var} to get a var_dump()-esque output for your variable.

Solution 4

just use {debug} in your .tpl and look at your sourcecode

Solution 5

In new Smarty it is:

<pre>
{var_dump($variable)}
</pre>
Share:
285,992

Related videos on Youtube

streetparade
Author by

streetparade

Updated on April 02, 2020

Comments

  • streetparade
    streetparade about 4 years

    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?

  • streetparade
    streetparade about 14 years
    sure i know that, but i need to do it without modding the core php files
  • Hobo
    Hobo almost 12 years
    Nice. This actually created a pop-up window for me, so I didn't have to look at the source. Had to disable my pop-up blocker though.
  • Damien
    Damien over 11 years
    A lot better then the chose answer.
  • thelem
    thelem over 11 years
    Resorting to php tags is not good practice and ideally they should be disabled for security reasons anyway. @debug_print_var (see answer from Chris) is a much better solition.
  • Luke Stevenson
    Luke Stevenson almost 11 years
    With the latest version of Smarty disabling the {php}...{/php} tags, Methods 2 or 3 are better options.
  • skobaljic
    skobaljic over 10 years
    Sometimes you are not sure what the variable is and many times @print_r and @var_dump did not work (in x-cart for example), but @debug_print_var output was there.
  • Alexander Kludt
    Alexander Kludt over 10 years
    Output looks even better if you surround it with <pre> tags. Methods above are the best.
  • ivanhoe
    ivanhoe over 9 years
    Add an additional param to print_r() to make it return the output to smarty, to avoid an extra echo at the end: {$var|@print_r:true}
  • Tana
    Tana over 6 years
    Thanks so much! Your answer is the only that works form me.
  • oliiix
    oliiix about 6 years
    better then the chosen answer? srsly? the chosen answer has those solutions in it too but just contains one more for older smarty versions, so I cannot really get how you could say it's better than the chosen one xD
  • Sharak
    Sharak over 2 years
    Most clean view of the variable gives {$var|@dump} and it doesn't even need to be wrapped with <pre></pre>. {$var|dump} works just the same on Smarty 3.