I am trying to get the content type to conditionally add JavaScript code/files to the template.

The code in template.php is the following one:

echo $node->type;

The same line in "node.tpl.php" is returning the content type. How do I get it to work in template.php?

  • 1
    In which function?
    – hpn
    Sep 29, 2011 at 19:19

2 Answers 2


You can put hook_preprocess_node() in your template.php.

function THEMENAME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if ($node->type == 'your_type') {
    //Add the JavaScript file.

The "template.php" file for a theme is the equivalent of a ".module" file for a module: It's a collection of functions that are invoked from Drupal in specific cases.

As you are referring to the "node.tpl.php" file, I guess you are adding a preprocess function in your theme. In that case, the variable to use inside THEMENAME_preprocess_node() is $variables['node'], which is the same variable available in THEMENAME_preprocess_page().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.