186

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?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
streetparade
  • 32,000
  • 37
  • 101
  • 123

14 Answers14

374

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}
algorhythm
  • 8,530
  • 3
  • 35
  • 47
pinaki
  • 5,393
  • 2
  • 24
  • 32
  • 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. – thelem Nov 21 '12 at 23:41
  • 1
    With the latest version of Smarty disabling the `{php}...{/php}` tags, Methods 2 or 3 are better options. – Luke Stevenson Jun 05 '13 at 11:55
  • 1
    Output looks even better if you surround it with
     tags. Methods above are the best.
    – Alexander Kludt Feb 07 '14 at 13:59
  • 3
    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} – ivanhoe Dec 13 '14 at 09:54
  • 1
    Most clean view of the variable gives `{$var|@dump}` and it doesn't even need to be wrapped with `
    `. `{$var|dump}` works just the same on Smarty 3.
    – Sharak Jan 22 '22 at 12:52
123

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.

Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
  • 8
    A lot better then the chose answer. – Damien Aug 15 '12 at 11:27
  • 1
    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 – oliiix Mar 14 '18 at 12:16
  • @oliiix Just take a look at the revisions. In 2012 the answer was far away from where it is now! – csabinho Nov 03 '22 at 09:13
29

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

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Chris
  • 758
  • 7
  • 8
  • 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. – skobaljic Nov 23 '13 at 00:37
  • Thanks so much! Your answer is the only that works form me. – Tana Nov 07 '17 at 16:35
13

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

david
  • 139
  • 1
  • 2
  • 3
    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. – Hobo Jul 11 '12 at 09:22
6

In new Smarty it is:

<pre>
{var_dump($variable)}
</pre>
Alexander Zakusilo
  • 1,466
  • 3
  • 16
  • 34
5

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

karadayi
  • 2,212
  • 2
  • 21
  • 36
2

try this .... Set $debugging to TRUE in Smarty.

RubyDubee
  • 2,426
  • 2
  • 23
  • 34
1

To debug in smarty in prestashop 1.6.x :

{ddd($variable)} -> debug and die

{ppp($variable)} -> debug only

An onther usefull debug tag :

{debug}
Aurelink
  • 11
  • 1
1

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

Bastilol
  • 21
  • 4
1

in smarty V3 you can use this

{var_dump($variable)}

Awais fiaz
  • 351
  • 1
  • 5
  • 20
1

I prefer to use <script>console.log({$varname|@json_encode})</script> to log to the console.

1

{$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!

r_a_f
  • 629
  • 7
  • 18
1

For better display :

{$var|@dump}
0

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)

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
Pranav Bhatt
  • 715
  • 4
  • 8