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

  • Holy **** Google #chrome is fast with Web Outlook 14 years 36 weeks ago
  • @gavinbrook @casablanca You could buy a better mac and put #ubuntu on it and then put project on it 14 years 36 weeks ago
  • I dream of a cross between, #Eclipse, #netbeans, #textmate, #kate and #kdevelop 14 years 37 weeks ago
  • Nothing better than a shish kebab after a long cycle ride home in the rain. Extra chilies please! 14 years 38 weeks ago
  • @philpeace Apparently in the Qwerty keyboard, the letters t-y-p-e-w-r-i-t-er are all on the top row to allow sales men to type it quickly. 14 years 38 weeks ago
  • Anybody know how to enable a views block programattically? #drupal 14 years 39 weeks ago
  • Nice, especially like the <new [content type]> option too ♻ @drupalninja: menu editor module: http://drupal.org/project/menu_editor #drupal 14 years 39 weeks ago
  • You know CSS hasn't been written correctly when you have to write more for the same thing on a different page. 14 years 39 weeks ago
Oliver Polden