Get all variables assigned to Smarty - Part 1
Posted September 24th, 2009 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 here which gives a couple more examples and shows how to return just one variable assigned to Smarty instead of everything.
Related posts:
- Get all variables assigned to Smarty - Part 2 (Monday, November 23rd 2009)
- Return information from PHP print_r instead of displaying it (Tuesday, September 30th 2008)
- Get a list of all available constants with PHP (Thursday, July 17th 2008)
- Get a list of all available classes with PHP (Thursday, July 10th 2008)
- Get a list of all available functions with PHP (Thursday, July 3rd 2008)
- Get unique array values with PHP (Saturday, December 29th 2007)
Share or Bookmark
Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.
Subscribe or Follow
Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

