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

  • My blog about 'Time is NOT money'... and value http://t.co/g2C6pZy0 12 years 11 weeks ago
  • I've found an excellent query logging and filtering module for Drupal. Not available via http://t.co/U1Xe92Th: http://t.co/2qvg1DgE 12 years 15 weeks ago
  • I've just spotted my first © 2011 at the bottom of a website. 12 years 15 weeks ago
  • @da_lune That's the spirit, oh, too late @gavinbrook, Wordpress is fantastic & now more than a blogging tool but Drupal = more functionality 12 years 19 weeks ago
  • @oliverpolden is having an amazing day for many reasons. I feel like giving back: http://t.co/JvKVVMBF Have a request? use the contact form. 12 years 19 weeks ago
  • The world's population is 7 billion. If a few 'competing' companies mutually helped each other get more customers business would be easy. 12 years 19 weeks ago
  • @Casablanca Amazing photos! Thank you for sharing. 12 years 20 weeks ago
  • Ahh, the Christmas Snow module for Drupal: http://t.co/QzvkQiT2 12 years 20 weeks ago
Oliver Polden