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

  • Ever wanted to create a CSS class or id from any old string in #Drupal? ctools_cleanstring() in the ctools module is your friend. 12 years 26 weeks ago
  • How to inspect the :active and :hover pseudo CSS style: http://t.co/zVANhKLp 12 years 27 weeks ago
  • @iainfarrell Good luck up north! 12 years 27 weeks ago
  • My most intriguing #Drupal utility modules out of 1030 for erm... Drupal 6, http://t.co/kmXV4qLp 12 years 27 weeks ago
  • #Chrome please can you check my hosts file before doing a search on an internet. Also, if I edit a url, yes that's still my local machine. 12 years 27 weeks ago
  • Seeing (not provided) in your Google Analytics?.. http://t.co/rSlpwm7U 12 years 28 weeks ago
  • Why SEO isn't evil http://t.co/MHh7ZHkK 12 years 28 weeks ago
  • @philhawksworth But that's unpossible! 12 years 29 weeks ago
Oliver Polden