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

  • Let down by #phpstorm :-( no icon decorations on folders for version control status. Vote for it here: goo.gl/U6zRU 13 years 47 weeks ago
  • Having fun with #javascript, I mean #jquery, actually, I mean both :-p 13 years 47 weeks ago
  • Bitten by godaddy again, after a long time trying to make sense of my account I see I finally have nothing left on it. Bye bye! 13 years 48 weeks ago
  • ooh, jet profiler for mysql is pretty sweet 13 years 48 weeks ago
  • Here's some light friday afternoon reading, 26 Terabits per second: goo.gl/64ClB 13 years 48 weeks ago
  • A quick blog about setting up automated #Selenium testing with #Jenkins goo.gl/RQgaC 13 years 49 weeks ago
  • #ubuntu #gnome #canonical What features do you love that a KDE user should know about? Looking to add to my blog: goo.gl/Xmlk2 13 years 49 weeks ago
  • @philhawksworth I get the feeling you're going to start talking to yourself soon! 13 years 49 weeks ago
Oliver Polden