How do I load templates within templates? Ok so I am writing a custom module and am trying to undestand how Drupal manages theming output.

I have registered my theme function, ie

mymodule_theme() {
    return array(
        'mylist' =&gt; 'templates/mylist-template',
        'arguments' =&gt; array('data'=&gt;NULL)
    );
}


The mylist-template contains a HTML table with a heading row, such as:

&lt;table&gt;
    &lt;tr&gt;
        &lt;td&gt;ID&lt;/td&gt;
        &lt;td&gt;Name&lt;/td&gt;
    &lt;/tr&gt;
    &lt;?php print $rows; ?&gt;
&lt;/table&gt;


As you can see here I want to output x number of rows using another template file such as list-rows.tpl.php, ie:

&lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;My Name&lt;/td&gt;
&lt;/tr&gt;


How do I achieve this? At the moment I have a menu item with my table function, let's say:

 function mymodule_table() {
     $output = theme('list','');
     return $output;
 }


This works fine but im not sure how I would implement my rows
You need to (properly) define two theme hooks. One for your table, and the second for your rows. Then, in the preprocess function for your table, you can call the row template for each row in your data array, something like:

function MODULE_theme() {
  return array(
    'mylist' =&gt; array('arguments' =&gt; array('data' =&gt; array())),
    'mylist_row' =&gt; array('arguments' =&gt; array('id' =&gt; NULL, 'name' =&gt; NULL)),
  );
}

function MODULE_preprocess_mylist(&amp;$variables) {
  $rows = array();
  foreach ($variables['data'] as $item) {
    $rows[] = theme('mylist_row', $item['id'], $item['name']);
  }
}


If you want to display a nice and themed table, you don't need to implement your own theme hooks using templates, the table theme hook provided by core should be fine. It is a complete and flexible solution for most table output needs.

function MODULE_table() {
  $header = array(t('ID'), t('Name'));
  $rows = array(
    array(1, 'foo'),
    array(2, 'bar'),
    array(3, 'foobar'),
  );
  return theme('table', $header, $rows),
}