1

I'm developing a module which should display a tpl.php file. Check this code:

/**
 * Implements hook_theme().
 */
function cookiedo_theme() {
  $path = drupal_get_path('module', 'cookiedo');
  return array(
    'cookiedo_info' => array(
      'template' => '/theme/cookiedo.theme',
      'path' => $path,
    ),
  );
}

There are no mistakes right? So i'd like to output/render cookiedo.theme.tpl.php, how should I do this?

this is all I can come up with.. :

  function cookiedo_preprocess_page() {

    cookiedo_theme('cookiedo_info'); 

}

I can possibly display this in a block/region, but I prefer to display the cookie.theme.tpl.php on all pages without the use of a block. The output should take place inside the .module file, I don't want the need to put extra code in the main template file.

I hope you understand my problem and question.

Thanks in advance for your help

1 Answer 1

7

That's not quite how hook_theme() works...Drupal's theme registry will pick up the themes you define in that function, and to call them you use the central theme() function.

You also need one of render element or variables in there (even if you're not passing any). The path defaults to the module that the hook is defined in so there's no need to specify. So your hook_theme() should look like this:

function cookiedo_theme() {
  return array(
    'cookiedo_info' => array(
      'template' => '/theme/cookiedo.theme',
      'variables' => array()
    ),
  );
}

And in your preprocess function you would call that theme like so:

$output = theme('cookiedo_info');

Remember to clear the caches once you make the change to the theme function or it won't get picked up by the registry.

2
  • thanks for your answer clive, I had to add print $output; to the preprocess function in order to make it work, is this the correct way?
    – mhmdrupal
    Commented Jun 21, 2012 at 7:17
  • 1
    You would normally use something like $vars['output'] = $output; in the preprocess function and then print $output; in page.tpl.php
    – Clive
    Commented Jun 21, 2012 at 19:04

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.