I am porting my old PHP site onto Drupal. I have a use case where one page needs to dynamically decide to use one of the many template files for presentation. Can this be done in Drupal? I've already written my own module, and implemented hook_theme as follows:

<?php
function mymodule_theme() {
  return array(
   
'mymodule_xxx_page'  => array(
     
'variables' => array('data' => null),
     
'template' => 'mymodule_xxx',    // use template file called "mymodule_xxx.tpl.php"
   
),
  );
}
?>

It is called from the callback function like:
$data = ......;
return theme('mymodule_xxx', array('data' => $data));

When I use a single template file everything works fine. I wonder if there is any way to define the 'template' property dynamically at runtime? It looks like I would have to somehow pass information into 'hook_theme' which does not look possible.

Comments

Replying my own question I

Replying my own question

I found a solution. Basically, I still use one single template, as specified in the 'template' property when defining my hook_theme. Then from the template file, I do:

require_once($template_page);

where the $template_page can be any file name, dynamically selected using my other business logic. The template file itself is a normal PHP so I suppose I can do all sorts of tricks in it.

Well I am not exactly separating data and presentation. But this approach seems to work for now. I am still interested if there are other more elegant methods to achieve this.