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

  • A great post about how the education system doesn't teach us how to be successful and follow our passions http://t.co/GsOpEUhj 12 years 22 weeks ago
  • @mattfarina how about em? 12 years 23 weeks ago
  • More about Omega here: http://t.co/kT7p1QSb 12 years 23 weeks ago
  • Delta + Omega + Context is probably one of the best things to happen to Drupal since Views 12 years 23 weeks ago
  • Wow! Just did a fresh install of Kubuntu Oneiric and I'm astonded. Very fast, very nice and lots of "oooh that's nice" 12 years 23 weeks ago
  • A great infographic about how Google works: http://t.co/dyZgxu3E 12 years 24 weeks ago
  • Just received my nexus 9000 silent mouse. First impressions, verrrrry nice, perhaps should have bought the 8000 tho: http://t.co/CzJB31bR 12 years 24 weeks ago
  • I've barely used GIT but already SVN seems archaic 12 years 24 weeks ago
Oliver Polden