Theme Variable Total Visibility

Need to know absolutely, positively, and without-a-doubt what theming variables are available to your template? Here's a trick for total visibility.

In my previous post, Take Control of Your PHPTemplate Variables, I described the _phptemplate_variables function in template.php. It's passed a parameter named $vars which is an associative array of all the variables that will be created in the template . . . which is exactly what we're looking for!

Since _phptemplate_variables allows us to create theme variables, we have a simple way to get the $vars data into the template where we can look at it. Just put the following code in your template.php file:

function _phptemplate_variables($hook, $vars) {
  return array('vars_data' => $vars);
}

And the following code in your template file:

<pre><?php print htmlentities(print_r($vars_data, 1)) ?></pre>

Regardless of whether it's a node, block or comment, you'll get a map of all the variables available to you. This is what it looks like for a node: Full Listing of $vars for a Node

Happy theming!

Comments

to doing things this way versus just doing:

<?php
print_r(get_defined_vars());
?>

but here is the code I intended to have above (bookended with opening and closing php tags):

print_r(get_defined_vars());

Thanks for mentioning get_defined_vars(). It's definitely an option people should be aware of.

To answer your question, it's a method of limiting the output.

Once you're in the template the variables are defined individually and not nicely contained in an array. get_defined_vars() gives you everything. This leaves one scrolling down a very long page picking out theme variables from the between _SERVER, _ENVIRONMENT and friends. Capturing $vars in a template variable nicely limits the display to just the theme variables.

Re the code tags, no code filter installed.