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

  • @iainfarrell oooh, better Ubuntu, that's nice 15 years 6 weeks ago
  • @philhawksworth: Looking for an amazing web developer to join @TheTeam with superb HTML, CSS and Javascript fu. Contact me if that's you. 15 years 6 weeks ago
  • @iainfarrell Do they use Photoshop in Canonical land? 15 years 7 weeks ago
  • Windows uninstall program: Checking for necessary disk space, oooook.... 15 years 7 weeks ago
  • @iainfarrell loving all the references 15 years 7 weeks ago
  • Glad that a week with far too much Windows pain is over 15 years 8 weeks ago
  • anyone ever had hook_mail not triggering? #drupal I've got mimemail, advanced contact and contact redirect installed 15 years 8 weeks ago
  • Enjoying my well spent £3 to listen to my #last.fm loved tracks for a month 15 years 8 weeks ago
Oliver Polden