I have template.php and node--product.tpl.php files. What should be my function name in template.php to pass variable from template.php to .tpl.php

Here is template.php code

function mytheme_process_product(&$variables){  **//What should be my function name here**
$variables['temp_var']="This is demo Temporary Variable for Perticular Product .Tpl.php";


<?php print $temp_var; ?>

And my .tpl.php file name is node--product.tpl.php where product is my content type name.

Please reply.


You need to use theme_preprocess_node function in template to add variables..

 * Override or insert variables into the node templates.
 * @param $variables
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("node" in this case.)

function YOURTHEME_preprocess_node(&$variables, $hook) {

  // define $node object, so it's be easier to use it
  $node = $variables['node'];

  // add variables to node--nameofnodetype.tpl.php file
  if ($node->type == 'product'){

      // $variables: an array with variables
      // you need to add one, so just define it
      $variables['my_variable'] = 'whatever';


In your node--product.tpl.php file:

// check if your new variable exist
print $my_variable;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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