1

What I am trying to do, is to set some variables in my function and to send this to my output.tpl.php.

My steps are the following.

  1. Declare my theme template

    function mymodule_theme() {
      return array(
        'my_file' => array('template' => 'output')
      );
    };
    
  2. Define the following function

    function mymodule_myfunction() {
      $build['variables'] = array(
        '#theme' => 'my_file', 
        '#variables' => $my_vars
      ); 
      return $build;
    } 
    
  3. I use the following code for my output file (output.tpl.php)

    <?php echo $my_vars[0]; ?>
    

I get an error saying $my_vars is unknown.

What am I doing wrong?

  • 2
    That isn't how to use theme functions at all - have you read the docs? – Clive Dec 11 '15 at 13:18
  • I can'f find a solution. If it is wrong, show me please what is wrong. My code works, but not $my_vars ouput of my variable. – user52660 Dec 11 '15 at 14:07
  • Sorry but there's too much wrong there for me to pick it apart in comments/an answer - read the documentation, copy out the examples into a test site so that you know your methods are correct, and then worry about actually trying to implement it. I mean no offence, but it looks a lot like you're guessing rather than basing your code on examples or official documentation – Clive Dec 11 '15 at 14:14
  • Check out this for learning how to theme correctly. To add variables to a 'correct' tpl file use preprocess functions. – sareed Dec 11 '15 at 14:18
  • I have read this documentation. I would like to gererate my variables in mymodule_myfunction() and not in template_preprocess_foo(). Can anybody to add existing code to make it workable? – user52660 Dec 11 '15 at 14:28
0

As described in Override template files based on template filename as well as hook name, if you theme function is my_file, your template file needs to be my-file.tpl.php; you cannot use output.tpl.php because Drupal would not recognize it as template file, at least until Drupal code is not changed as per the issue I linked.
The variables that you pass to the template file needs to be defined in your implementation of hook_theme().

Your code should be similar to the following one.

function mymodule_theme() {
  return array(
    'mymodule_output' => array('template' => 'mymodule-output', variables => array('my_vars' => NULL))
  );
};

function mymodule_myfunction() {
  // Add here the code that initializes $my_vars.
  // …

  $build['variables'] = array(
    '#theme' => 'mymodule_output', 
    '#my_vars' => $my_vars
  ); 
  return $build;
} 
// mymodule-output.tpl.php
<?php echo $my_vars[0]; ?>

I used mymodule_output as name of the theme function and mymodule-output.tpl.php as name of the template because the coding standards used from Drupal suggests to use the module name as prefix; it would not change if you used output where I used mymodule-output or mymodule_output.

If all you are doing is printing a variable, then you don't need a theme. Theme functions (and template files) are used when you plan to allow themes or other modules to alter the output of your module. That is why Drupal uses node as theme function: to allow themes to alter how a node is rendered. The Node module defines its own node.tpl.php file, and the Bartik theme uses its own node.tpl.php.

-1

To simplify the task use the following code.

function mymodule_myfunction() {
  $my_vars = array()
  $my_vars[0] = 'bla bla bla';
  $my_vars[1] = 'some other value';
  return theme('my_file', $my_vars);
} 

And your theme function will look like

function mymodule_theme() {
    return array(
        'my_file' => array(
            'template' => 'output',
            'variables' => array(),
         )
    );

}

Now in output.tpl.php you simply print

<?php print $variables[0]; ?>
  • Hmm. Sorry, it doesn't work – user52660 Dec 11 '15 at 16:32
  • This is all kinds of wrong... – Clive Dec 11 '15 at 16:33
  • @Clive I am doing it in many modules, May I know why? I am asking just to learn – Shabir A. Dec 11 '15 at 17:27
  • Assuming Drupal 7...variables should be named in hook_theme, e.g. 'variables' => array('foo' => NULL), passed to a render array like $build = array('#theme' => 'bar', '#foo' => 'baz');, and used in the template like <?php echo $foo; ?>. I'm not sure why your code would work, but it's not the 'normal' way of doing things – Clive Dec 11 '15 at 18:12
  • 1
    Which is bad @ShabirA. - it breaks caching, and makes your code harder to upgrade. Do a quick search for "drupal theme v render array" and read the top 5 links or so, they explain it better than I could in comments – Clive Dec 11 '15 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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