Get All Template Variables in a Drupal TPL File
A quick tip for Drupal theming: how to inspect all available template variables.
Get All Template Variables in a Drupal TPL File
Note: This is a restored article from the original A thousand nodes blog (circa 2013).
When working on Drupal themes, it's often helpful to see all available template variables. Instead of digging through documentation or guessing what might be available, you can use a simple PHP function to reveal everything.
The One-Line Solution
In any Drupal template file (*.tpl.php), you can add this code to see all available variables:
<?php print '<pre>' . print_r(get_defined_vars(), TRUE) . '</pre>'; ?>
When to Use This
This technique is particularly useful when:
- Creating a new custom theme
- Overriding a template file from a module
- Debugging theme issues
- Learning how Drupal's theming system works
Example: Node Template
If you add this code to your node.tpl.php
file, you'll see all variables available for that node, including:
$title
: The node title$content
: The rendered content$node
: The full node object$page
: Boolean indicating if this is a full page view$teaser
: Boolean indicating if this is a teaser view- And many more!
Example: Block Template
In a block.tpl.php
file, you'll see variables like:
$block->subject
: The block title$block->content
: The block content$block->module
: The module that created the block$block->region
: The region where the block is placed- And more!
Important Notes
-
Remove in Production: Always remove this code before deploying to production! It exposes all variables, which might include sensitive information.
-
Performance Impact: This function can output a lot of data and impact performance, so use it only during development.
-
Alternative for Drupal 8/9: If you're using Drupal 8 or higher with Twig templates, use the Twig debug mode or the
{{ dump() }}
function instead:
{{ dump() }}
A More Selective Approach
If you want to look at specific variables instead of everything:
<?php
// Just dump the node object
print '<pre>' . print_r($node, TRUE) . '</pre>';
// Or just the content array
print '<pre>' . print_r($content, TRUE) . '</pre>';
?>
This simple debugging technique has saved me countless hours when working on Drupal themes, especially when dealing with complex modules that provide many template variables.