module_get_template_content()
DEFINITION
function module_get_template_content($module_name, $type, $template_name, $region = '', $vars = '')
This function returns the output of a template file and any logic of the custom_template.php file as well.
$module_name = Your module's SHORT machine name (actually the directory name your module uses)
$type = Can be one of block, node, page, or any thing you desire like store, sometype, keyboards, beach.
$template_name = A unique identifier for the filename.
$region = This is similar to block regions except that it can be used for any type as well. This is optional.
$vars = An array of variables you want to be available to the template. This MUST be an array. If it isnt, it attempts to convert it to an array. So you 'can' send it an object and it'll just recreate the object as an array but thats probably not the best way to do things. It will also convert strings/integers etc into an array by using the variable's type as the element key.
Examples
These 2 examples show 2 different 'types' as well as one that has a region and the other doesnt. I dont recommend using region for anything other than blocks but you can use them however you like just the same. To see how $region is used correctly look at the 3rd example.
function yourModule_output_some_page()
{
//Display either $current_template_path/yourModule-block-searchform-tpl.php OR $yourModulePath/yourModule-block-searchform-tpl.php
$contents = module_get_template_content('yourModule', 'block', 'searchform', $region = '', $vars = array('somevarname' => 'itsvalue'));
return $contents;
}
//Display either $current_template_path/yourModule-cars-searchform-left-tpl.php OR $yourModulePath/yourModule-cars-searchform-left-tpl.php
$contents = module_get_template_content('yourModule', 'cars', 'searchform', $region = 'left', $vars);
return $contents;
This 3rd example shows how to correctly use the region variable on a block. It defines a block, grabs the block's region and then sends back the output for the block. You can then change the region and have different template files for EACH region. This makes blocks SUPER themeable. I use block a lot ;)
//Implementation of hook block -Ryan
function yourModule_block($op = 'list', $delta = 0, $edit = array())
{
if ($op == 'list')
{
$blocks[0] = array('info' => t('yourModule Search'),
'weight' => 0, 'status' => 0, 'region' => 'right');
return $blocks;
}
if ($op == 'view')
{
if ($delta == 0) //Search Block -Ryan
{
//figure out which region the search block is currently assigned to because there are multiple templates for each region. -Ryan
$theme = basename(path_to_theme()); //Grab the theme name. Dont know how else to do this. -Ryan
$query = "SELECT * from {blocks} WHERE module='yourModule' AND delta='0' AND theme='$theme'";
$results = db_query($query);
$rows = db_fetch_object($results);
$region = $rows->region;
$block = array('subject' => t('Search Cars'),
'content' => yourModule_search_block('block', 'searchform', $region), 'enabled' => 0);
}
return $block;
}
}
function yourModule_search_block($type, $template_name, $region)
{
//create a dropdown and send it into the template as a variable
$vars['tax_dropdown'] = taxonomy_form($vid = 2);
$vars['anothervar'] = 'some value';
$vars['itsAnObject']->somethin = 'its an object?!';
//output the file $current_theme/yourModule-block-searchform-$region.tpl.php with $vars available OR modules/yourModule-block-searchform-$region.tpl.php with $vars available if the theme version didnt exist.
$contents = module_get_template_content('yourModule', $type, $template_name, $region, $vars);
return $contents;
}
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion