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...

  • search for 'search' on google.com and bing comes up first!?!?!?! 13 years 32 weeks ago
  • A genius, high value tip/test for #Drupal #continuousintegration with #Jenkins and #selenium if I do say so myself goo.gl/IGIy5 13 years 32 weeks ago
  • @philhawksworth Damn it! Someone beat me to it! 13 years 33 weeks ago
  • A short post I just wrote on setting up #Memcache for #Drupal: goo.gl/2VnUc 13 years 34 weeks ago
  • Seems ftp.drupal.org is down, but you can still download modules by hacking the download link to remove ftp. #drupal 13 years 34 weeks ago
  • dear #lazyweb #phpundercontrol ( #cruisecontrol ) or #jenkins for #drupal #continuousintegration 13 years 35 weeks ago
  • You've got to have some nuts to do this: goo.gl/TKqIe 13 years 35 weeks ago
  • http://t.co/q8CiVNc Dratman #drupal 13 years 36 weeks ago
Oliver Polden