0

When using hook_theme I create an array of variables with default values:

/**
 * Implementation of hook_theme
 */
function my_module_theme() {

  return array(

    'my_theme' => array(
      'arguments' => array(
        'var1' => 'default value',
      ),
      'template' => 'my-theme',
    ),

  );
}

And the template file my-theme.tpl.php:

<div><?php print $var1; ?></div>

But inside the corresponding template the $var1 variable is only recognised if I explicitly send it with the theme() implementation:

print theme('my_theme', array('var1' => 'New var'); // No errors

print theme('my_theme'); // Notice: Undefined variable: var1 in include()

My understanding of the default arguments are just that - they should be the default if they are not explicitly defined. What am I missing?

2 Answers 2

1

You have to do like

/** * Implementation of hook_theme */ function my_module_theme() {

return array(

'my_theme' => array(
  'variables' => array(
    'var1' => 'default value',
  ),
  'template' => 'my-theme',
),

); }

Instead of arguments use variables.

Hope this will help.

0
0

If you want your variable to take default value then you first need to define that variable in your_theme_preprocess_HOOK() function then only your variable will be available in template file without any php notice.

Your Answer

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

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