Hi

I'm trying to learn about theme functions by making this very simple function in my module:

function theme_snippet($variables) {
  drupal_set_message(t('My function is being called'));
  $output = '<h2>' . t('It works!') . '</h2>';
  return $output;
}

Then I call my function in template.php:

function mytheme_preprocess_node(&$variables) {
  $variables['snippet'] = theme('snippet');
}

And in my node.tpl.php:

if (isset($snippet)):
  print $snippet;
endif;

But nothing happens, and because the message doesn't display I don't think my function is getting called?

Thanks!

Comments

chrlvclaudiu’s picture

As far as i can tell, your theme is not registered. have you registered your theme ?

example:

<?php
function yourmodule_theme($existing, $type, $theme, $path) {
  return array(
    'snipet' => array(
      'variables' => array('var1' => NULL, 'var2' => NULL, ....),
    ),
 );
?>

Check here:
D7: http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_theme/7
D6: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_theme/6
Don't forget to clear cache after you register the theme.

kirel’s picture

Ah I had forgotten to do that. Now it is working :)