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

  • Here you go @leisa 3.ly/cH6X 14 years 6 weeks ago
  • @leisa you need one of these: 3.ly/BVNU 14 years 6 weeks ago
  • Delving deep into the Drupal services module, where exactly, and on which side is my code failing? Do like a challenge tho! 3.ly/2AcM 14 years 6 weeks ago
  • Got out a new blank dvorak keyboard at my new place. Wasn't long to get the response I usually get :-) 14 years 6 weeks ago
  • @casablanca Budgens did it :-) Time for a bit of Urban Terror in a few minutes http://www.urbanterror.info 14 years 6 weeks ago
  • Beer O'Clock in an hour :-) ... oh :-( 14 years 6 weeks ago
  • I'm not religious but statements in the bible seem to have been accurate or at least reasonable for hundreds of years. 14 years 8 weeks ago
  • I clicked a link to this on another site. Is it a joke? http://protectyourpcdata.co.uk/PDF/Data_Loss_Quotes_and_Statistics.pdf 14 years 10 weeks ago
Oliver Polden