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

  • Day one of my Drupalipet from the Drupal 7 release party. http://twitgoo.com/1ta7eo 14 years 16 weeks ago
  • At Drupal 7 release party. Can't wait to start using it. Didn't realise how much Microsoft is supporting it! #d7rp_london 14 years 17 weeks ago
  • Drupal 7 is out and I'm the third that commented! http://buytaert.net/drupal-7.0-released #drupal7 14 years 17 weeks ago
  • http://twitgoo.com/1sfadf my attempt at a snowman 14 years 18 weeks ago
  • I signed the "KDE4 as the default environment in Ubuntu" petition! http://petol.org/ubuntu4 #petol 14 years 24 weeks ago
  • @eventsacademy will you be putting your video up on youtube? 14 years 35 weeks ago
  • Extremely impressed with @eventsacademy tonight. Simply amazing what they have achieved. More on the site http://eventsacademy.org.uk/ 14 years 35 weeks ago
  • Ooh... a very compelling reason to finally get a new (Google) tv http://bit.ly/c91D6x 14 years 35 weeks ago
Oliver Polden