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

  • Wohoo, got my first #selenium login test running from #jenkins/#hudson 13 years 50 weeks ago
  • That's a really annoying ad, thanks #microsoft goo.gl/oX62O 13 years 50 weeks ago
  • #windows #mac #linux Can you turn your volume up/down using your mouse wheel on your system tray icon? Can you mute it with middle click? 13 years 50 weeks ago
  • Just received my Web Designer's Idea Book. Very worthwhile investment, although there's a two volume set I should have bought! 13 years 50 weeks ago
  • Interesting article about click distribution over search results: goo.gl/WOBRU #seo 13 years 51 weeks ago
  • Trying to find the apache vhost configuration for a site? apache2ctl -S 13 years 51 weeks ago
  • One small step for Google, one giant leap for IE6, Chrome Frame without needing admin rights: goo.gl/kBkEs /via @lmjabreu 13 years 51 weeks ago
  • @philhawsworth @casablanca @a_alfredo suddenly having a big pang of fussball mourning 13 years 51 weeks ago
Oliver Polden