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

  • @casablanca @philhawksworth ohhhh, ahhhh, argh! 14 years 3 weeks ago
  • What the? Updating two packages on Kubuntu... 4 minutes remaining... oh, one of them is #Skype. 14 years 4 weeks ago
  • Thanks @gavinbrook Seems #woopra is the only one that does more than monitoring. The tagging users feature looks very useful. 14 years 4 weeks ago
  • Any votes for: #woopra #chartbeat #mixpanel or #clicky or any other #realtimeanalytics suggestions? 14 years 4 weeks ago
  • Ohh, just having a look at the git instructions for my own Drupal project. Learning a lot about git in the process : -) goo.gl/ZKBo7 14 years 4 weeks ago
  • @philhawksworth @philpeace I'm slowly moving all my sites off Godaddy too but they don't have a proper way of transferring .co.uk sites!!! 14 years 4 weeks ago
  • enjoying a bit of javascript but what am I doing at late so work? 14 years 5 weeks ago
  • [GitHub] theteam added you to a team, lol 14 years 5 weeks ago
Oliver Polden