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

  • Very impressed with yell's 3D maps, simply amazing: http://www.yell.com/maps/ 15 years 4 days ago
  • So much compelling about #ubuntu #lucid, but at the same time, stuff I can't live without in #kubuntu that deffinitely isn't in ubuntu 15 years 2 weeks ago
  • Scratch that! #ubuntu does have mouse wheel volume control, just not with the thingy open ooooooh! 15 years 2 weeks ago
  • Ok, lucid #ubuntu still doesn't let you control volume with your mouse wheel via the system tray icon, dissappointing 15 years 2 weeks ago
  • The Gimp resynthesizer plugin came in handy quicker than I thought it would: http://2tu.us/1xx3 15 years 3 weeks ago
  • Ohh, content-aware fill in #CS5, oooooooohhhhhhh, resynthesizer already been in Gimp for years: http://2tu.us/1xx3 15 years 4 weeks ago
  • @drupalninja There's a module for that! http://2tu.us/1xw4 15 years 4 weeks ago
  • See, programming by trial and error is foolproof... if you don't give up. 15 years 5 weeks ago
Oliver Polden