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

  • Yes. If you want to torture me put me on a locked down windows computer with a locked down network and no power for a day. 15 years 8 weeks ago
  • can someone please #invent a mobile phone where the camera can double as an optical mouse 15 years 10 weeks ago
  • @philhawksworth don't you mean thank goodness for @oliverpolden too :p 15 years 10 weeks ago
  • What's #Debian like nowadays compared to #Ubuntu ? 15 years 10 weeks ago
  • @gavman80s Pidgin will do all that and more and has done for years: http://2tu.us/1pha 15 years 10 weeks ago
  • that doesn't work if no other checkboxes are checked by default #drupal contact-us?edit[submitted][mycheckboxes][0]=mycheckbox 15 years 13 weeks ago
  • ahh, something like: contact-us?edit[submitted][mycheckboxes][0]=mycheckbox 15 years 13 weeks ago
  • Is it possible to use the prepopulate module to check a checkbox on a webform? #drupal 15 years 13 weeks ago
Oliver Polden