Get all variables assigned to Smarty - Part 1

Posted in PHP -

I've been working with the PHP templating engine Smarty at a new job and needed to be able to dump a list of variables that had been assigned to Smarty for debugging purposes. This quick post shows how to dump the variables in your PHP code.

get_template_vars()

Smarty's get_template_vars() function returns the variables assigned using the assign() function as an associative array where the array key is the name of the variable and the array value is the variable value.

You can also get the values assigned from the config using get_config_vars() and the usage is exactly the same as illustrated here.

Assuming $smarty is a Smarty instance, use the following code to dump the assigned variables using the print_r() function:

print_r( $smarty->get_template_vars() );

The data could instead be returned into a variable and something else done with it:

$variables = $smarty->get_template_vars();

Part 2

I have written a follow up to this post which gives a couple more examples and shows how to return just one variable assigned to Smarty instead of everything.



Related posts:


Comments