theme() parameters sent to output.tpl.php 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.


Declare my theme template

function mymodule_theme() {
  return array(
    'my_file' =&gt; array('template' =&gt; 'output')
  );
};

Define the following function

function mymodule_myfunction() {
  $build['variables'] = array(
    '#theme' =&gt; 'my_file', 
    '#variables' =&gt; $my_vars
  ); 
  return $build;
} 

I use the following code for my output file (output.tpl.php) 

&lt;?php echo $my_vars[0]; ?&gt;



I get an error saying $my_vars is unknown.

What am I doing wrong?
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' =&gt; array('template' =&gt; 'mymodule-output', variables =&gt; array('my_vars' =&gt; NULL))
  );
};

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

  $build['variables'] = array(
    '#theme' =&gt; 'mymodule_output', 
    '#my_vars' =&gt; $my_vars
  ); 
  return $build;
} 




// mymodule-output.tpl.php
&lt;?php echo $my_vars[0]; ?&gt;


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.