0

I have a page.tpl.php in which has header , footer and content area. I need to load different content base on hook_menu from a module.

I am using the following test code in the module to try and print something from my template:

function my_module_theme() {
  return array(
    'tutorials_test' => array(
    'template' => 'tutorial'
    )
 );
}

I have a template tutorial.tpl.php in the modules folder

The following is my hook_menu and the callback function

function my_module_menu() {
      $items['insights/tutorials'] = array(
         'title' => 'Tutorials',
         'access callback' => TRUE,
        'page callback' => 'insights_tutorials'
  );
}

The callback function

  function insights_tutorials() {
   echo 'test';
   print theme('tutorials_test');
   echo 'after test';
  }

When I turn to that page i can see the text 'test' and 'after test' but nothing from my template is printed.

tutorial.tpl.php has this simple code:

<h1>Hello World</h1>
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
Abhishek
  • 157
  • 1
  • 4
  • 13

1 Answers1

1

Inside your hook_theme implementation (the function my_module_theme) you need to pass in the variables key

function my_module_theme() {
  return array(
    'tutorials_test' => array(
        'template' => 'tutorial',
        'variables' => array( // the variables key
            'title' => NULL,
            'details' => NULL,
        ),
    )
 );
}

Then output your HTML like that:

print theme('tutorials_test', array(
    'title' => 'This is the title',
    'details' => 'And this is details',
));

For a bitter example about how to implement hook_theme(), take a look at this answer.

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
  • Make sure you check out the answer provided in the last line. And don't forget to flush the caches after all the changes you make. – Muhammad Reda Nov 17 '12 at 10:00
  • I tried the code in a new test module and its working so somethings wrong in the existing module. Nothing wrong with the code though. Thanks. – Abhishek Nov 17 '12 at 12:27
  • My module name was same as my theme name and the code isn't working in that module. Its working in any other module – Abhishek Nov 17 '12 at 14:46