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

  • @Casablanca Looks like a great time last night. Sorry I couldn't make it :( 13 years 26 weeks ago
  • Our new homepage is live http://t.co/iwNX1tWZ 13 years 26 weeks ago
  • Can you help with conditionally setting the 'from' in mails sent with notifications/messaging modules #drupal http://t.co/ItE75IlW 13 years 27 weeks ago
  • My post about highjacking #Drupal menu items with 'drupal_get_form' callbacks for creating popups or #ahah goodness http://t.co/Pe9wSz6k 13 years 27 weeks ago
  • Oh the conflict within. An entire section of a #drupal site was built bespoke, do I add a bespoke subsection, or rewrite the entire section? 13 years 30 weeks ago
  • @psd It depends. Serving static pages means https has a larger relative impact. If there's a lot of processing, https is relatively small. 13 years 31 weeks ago
  • Just had a play with the #drupal Field collection module. goo.gl/p6sEn A really nice Drupal 7 implementation of the multigroup module. 13 years 31 weeks ago
  • @philhawksworth @a_alfredo When will scampcat have the descriptions pop up when you hover over the markers? 13 years 32 weeks ago
Oliver Polden