I have a custom module that takes a few arguments via the url, does some work on them and produces page content by returning the result of theme() as $content and this is inserted into page.tpl.php as standard.

Most of the time the themes standard page.tpl.php suffices but I have a need to be able to return the content in a different page.tpl.php depending on one of arguments passed or a calculation that the module does based upon the arguments and database data.

Can someone please tell me the correct way to do this or direct me to an example of this?

Comments

prakashp’s picture

You can implement the preprocess page function in your module or theme. So for example you would implement the following in your module:

function modulename_preprocess_page(&$vars) {
  // depending on your condition of arguments and database values set the template_file variable
  if ($condition_met) {
    $vars['template_file'] = 'page-custom';
  }
}

This way if your condition is met, the output of your page will be rendered through page-custom.tpl.php. You'll have to clear the cache for your new template file to be picked up.

duckzland’s picture

or you can use :

function phptemplate_preprocess_page(&$variables) {
    if ( arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit' ) {
	$variables['template_files'][] = 'page-yourcustom';
	}
}

in the template.php to select the page.tpl based on url

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

ollie222’s picture

Thanks for the replies and using the above preprocess_page methods I can place a 'page-custom.tpl.php' file in the theme folder and it does work however I have a couple of issues with it.

1. How can I make it pick up a custom template file in the modules folder not the theme's? While not essential for this project it would be nice to be able to package a page template with module.

2. The main part of the module already performs the queries to work out if the page template needs changing and it seems a bit wasteful to have to perform these same operations in the preprocess_page hook. Is it possible to set the template file from the main part of the module or at least pass the result to the preprocess_page hook?

Thanks for the help on this, it's much appreciated.