Pass variables from custom module to its template?
To use the theme API it requires three portions: the template file, the theme hook, and then displaying the defined theme. The below examples are for Drupal 7.

&lt;?php
  // defines the theme hook - should follow the naming format "_theme()"
  function mymodule_theme(){
    return array(
      'example_template1' =&gt; array(
        'template' =&gt; 'mytemplatefile1', // mytemplatefile1.tpl.php
      ),
      'example_template2' =&gt; array(
        'template' =&gt; 'mytemplatefile2', // mytemplatefile2.tpl.php
      ),
    );
  }

  // in this function is where you would do your DB queries,
  // and pass to the theme function as shown
  function mymodule_customfunction(){
    $calculation = 1+1;
    return theme('example_template1', array(
      'example_variable1' =&gt; $calculation,
      'example_variable2' =&gt; '123',
    ));
  }
?&gt;


The mytemplatefile1.tpl.php file could look something like this:

&lt;div class="example-markup"&gt;&lt;?php echo $example_variable1; ?&gt;&lt;/div&gt;


To display your output, you could then just execute the following and it would print your value, wrapped in the "example-markup" div.

&lt;?php echo mymodule_customfunction(); ?&gt;