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

  • http://t.co/oHKBnJn Dratman 13 years 36 weeks ago
  • 3 mobile email support not very fast! "We’ll get back to you within ten days" 13 years 36 weeks ago
  • Just learned about the great advantages of 'Using Per-Table Tablespaces' for innodb mysql tables. goo.gl/Xm8KZ Reclaiming disk space & more 13 years 36 weeks ago
  • Oooh, Amazon Kindle cloud reader: http://t.co/RYRLw68 13 years 37 weeks ago
  • Just pointed out to my boss: "You spelt 'gobbledegook' wrong." 13 years 38 weeks ago
  • OMG! opening <div>s in various files, and closing </div>s in various other files! Time to clean that up! 13 years 39 weeks ago
  • How come only now after all these years am I using the #Drupal format_plural() and Drupal.formatPlural() functions? 13 years 39 weeks ago
  • Why do I see so much custom #drupal code with comments such as: "Sorry I had to do it like this because Drupal won't do it" Yes it will! 13 years 40 weeks ago
Oliver Polden