Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need to know the simplest method to pass variable from custom module to its template file. I have created the custom.module and placed custom.tpl.php in the module folder.

function custom_menu(){
  $items = array();

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function custom_page() {

    $setVar = 'this is custom module';
    return theme('custom', $setVar);    
}

i have added theme function but it is not working, can any one suggest me what is wrong with this code

function theme_custom($arg) {
  return $arg['output'];
}

function custom_theme() {
  return array(
    'Bluemarine' => array(
        'variables' => 'output',
        'template' => 'Bluemarine',
     ),
  );
}
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Independently from the Drupal version for which you are writing the module, there are two errors in your code:

  • You define "Bluemarine" as theme function, but then you call theme('custom'), which would call the "custom" theme function
  • If you define "custom" as a theme function that uses a template file, then theme_custom() is never called

If you are writing code for Drupal 6, then the code should be similar to the following one. I take the assumption the name for the theme function is custom.

function custom_menu(){
  $items = array();

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function custom_theme() {
  return array(
    'custom' => array(
      'arguments' => array('output' => NULL),
      'template' => 'custom',
     ),
  );
}

function custom_page() {
    $output = 'This is a custom module';
    return theme('custom', $output);    
}

function theme_custom($output) {
}

The template file will have access to $output, and to any variables set in template_preprocess_custom(), if your module implements it.

For example, you could implement code similar to the following one:

function template_preprocess_custom(&$variables) {
  if ($variables['output'] == 'This is a custom module') {
    $variables['append'] = ' and I wrote it myself.";
  }
}

With this code, the template file has access to $output and $append.

As example of theme function that uses a template file, you can look at theme_node(), which is defined in node_theme(), and that uses node.tpl.php as template file; the preprocess function implemented by the Node module for that theme function is template_preprocess_node().

share|improve this answer
    
Thanks Kiam, obviously missed OP's first comment about using D6. +1 –  Laxman13 Nov 5 '11 at 20:19
    
Thanks Kiam, template_preprocess_custom(&$variables) did the trick –  Kamran Akhter Nov 10 '11 at 10:01
    
Now I want to move tpl to theme folder, how can i do that? –  Kamran Akhter Nov 10 '11 at 10:07
    
@KamranAkhter That is a different question. :-) –  kiamlaluno Nov 10 '11 at 10:08
add comment

You are calling the wrong theme function. Instead of function theme_custom it should be function theme_Bluemarine. You also need to pass an array to the variables piece of hook_theme(). See a simple example here.

Using your example (after changing template and theme function to custom):

function custom_menu(){
  $items = array();

  $items['custom'] = array(
    'title' => t('custom!'),
    'page callback' => 'custom_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function custom_page() {
  $setVar = 'this is custom module';
  return theme('custom', array('output' => $setVar));
}

function custom_theme() {
  $path = drupal_get_path('module', 'custom');
  return array(
    'custom' => array(
        'variables' => array('output' => null),
        'template' => 'custom',
     ),
  );
}

Now in custom.tpl.php just need <?php print $output; ?>

share|improve this answer
    
Thanks for reply the example u gave is node module and i have non node module.i just need a way to pass variable to tpl file, i m using drupal 6.plz help –  Kamran Akhter Nov 1 '11 at 13:03
    
Thanks for reply, it is giving me the error No such file or directory actually it is expecting the file custom.tpl.php in the module folder which i already have so in code bluemarine will be replcaed by custom. Bluemarine is the drupal theme which i m using. –  Kamran Akhter Nov 1 '11 at 13:23
    
changed the above code and giving me the white blank screen what should i do plz help –  Kamran Akhter Nov 1 '11 at 13:28
    
I changed the code, and I have it working on my site. –  Laxman13 Nov 1 '11 at 13:39
    
I just copy paste the above code but it does not work –  Kamran Akhter Nov 1 '11 at 13:52
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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