How to get the variables available in a Drupal template

This is such a basic PHP function but easy to forget and hard to find for some reason. Anyway, here are a selection of the best ways to find what variables are available in a template file.

First off, the function you need is:

get_defined_vars();

The quickest way to use it is to put the following in your template file:

print_r(get_defined_vars());

Next, install the devel module. Once installed you will now have a 'Devel' tab available when viewing nodes (if you have the correct permission set up).

With the devel you also get a few handy functions:
Print a nicely formatted array to the message area of the page:

dpm(get_defined_vars());

Dump the variables to a temporary log file: /tmp/drupal_debug.txt

dd(get_defined_vars());

To watch the file in your terminal:
tail -f /tmp/drupal_debug.txt

or
tailf /tmp/drupal_debug.txt

Post new comment

By submitting this form, you accept the Mollom privacy policy.

User login

Author of...

  • Send or Forward module? #Drupal 15 years 15 weeks ago
  • Lol it's Friday: "It's not often you need to wipe and talk at the same time." 15 years 16 weeks ago
  • @timmillwood Looks like what you want exists but for Adriod http://2tu.us/1g6i 15 years 16 weeks ago
  • @drupalninja - I subscribe to the drupal modules feeds http://2tu.us/1fp5 15 years 16 weeks ago
  • @leisa Have you got some sort of cron functionality set up? 15 years 17 weeks ago
  • Enjoying the comments from @philhawksworth and @casablanca about my open source, drunken, Jedi, Rainman, mind, memory tricks last night 15 years 30 weeks ago
  • @philpeace, me too 15 years 32 weeks ago
  • Great article why a company moved from Joomla to Drupal http://2tu.us/uyn 15 years 32 weeks ago
Oliver Polden