1

I have a custom module which initiates the theme hook - passing the $node object as an argument:

theme('my_widget', array($node)); 
function my_module_theme() {
  return array(
    'my_widget' => array(
      'template' => 'my_widget',
      'variables' => array('node' => array($node)),
    ),
  );
}

However, in the 'my-widget' template file, I need to pick up the $node variable. But doing a dpm() on $node returns Null.. - think I may be setting the variable incorrectly, but not sure what's going on. Cheers

1 Answer 1

6

The correct code is the following one.

  • Theme call

    theme('my_widget', array('node' => $node)); 
    
  • Theme declaration

    function my_module_theme() {
      return array(
        'my_widget' => array(
          'template' => 'my_widget',
          // This array contains the default values.
          // You should not use a PHP variable.
          'variables' => array('node' => NULL),
        ),
      );
    }
    
2
  • how to print this variable in tpl file (ie) in my_widget.tpl.php Commented Sep 4, 2017 at 11:17
  • @user3386779 print it like this <?php print $node; ?> Commented Aug 5, 2019 at 9:37

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.