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

  • Did you know today is world peace day? http://t.co/5woHsuaQ 11 years 30 weeks ago
  • If you would like to donate to Sport relief and change people's lives the address is: http://t.co/riQCnQEq 12 years 4 weeks ago
  • Why big businesses cannot compete with small businesses: http://t.co/dNaQVJv3 12 years 7 weeks ago
  • The newest addition to my kindle: 'Know Me, Like Me, Follow Me' - Social networking for business by @pennypower http://t.co/hPVMRQKe 12 years 7 weeks ago
  • Using YouTube videos to improve your exposure and credibility http://t.co/rLKx6ByY 12 years 7 weeks ago
  • Penny power making great points about social media being personal #kpiday 12 years 8 weeks ago
  • @philhawksworth Yes, I lust after kate 12 years 9 weeks ago
  • Monday the 13th is the new Friday 13th. 12 years 9 weeks ago
Oliver Polden