Home / var_dump or print_r inside a Smarty template

var_dump or print_r inside a Smarty template

While trying to debug why something wasn’t working, I needed to see what data had been assigned to a Smarty template but from within the template itself (I didn’t have time to try and work out where in the PHP code the stuff was assigned and debug from there). This post shows how to do a var_dump or print_r from within the Smarty template itself.

Using {$var|print_r}

It is possible to do this in the template, where $var is the name of the variable:


but the downside is that it only outputs the values and not the keynames, and there are no spaces between the values, so it’s not ideal for debugging.

Using a {php} block

So you can cheat instead and throw in a {php} block and simply execute some PHP code. The following code again gets the variable assigned as $var:

  $myvar = $this->get_template_vars('var'); var_dump($myvar);


  $myvar = $this->get_template_vars('var'); print_r($myvar);

You might want to enclose it with a <pre> block to make it more readable in the browser.

Dumping the values from PHP code

I’ve shown in the past how to dump the variables assigned to a Smarty template with PHP. See the related posts below for more details.