Too Much?! more power from your node.tpl
The less tpl files the better. Don't get me wrong... LOVE the tpl file, can't live without the tpl file... If I need several tpl files, let's do it... but, in some cases, one tpl file can provide enough power and flexibility to handle several content types, and keep the job of a themer a little easier.. o.k., not easier, but a little cleaner for my tastes.
The magic is the preprocess functions. Here's a little about the preprocess function.
Preprocess functions only apply to theming hooks implemented as templates. The main role of the preprocessor is to setup variables to be placed within the template (.tpl.php) files. Plain theme functions do not interact with preprocessors. http://drupal.org/node/223430
So, it allows the themer to create some thing, define it as a variable (and sometimes an action) and have it execute when the page/node loads. One feature I like is identifying the node and making things happen before it is presented...
some examples....
By default the node type 'housing' would pick-up and use a template 'node-housing.tpl.php' and the node type service would grab 'node-service/tpl.php'... but I want them both to use the same template.
IF NODE IS!!!
//Template Switching
$type = $vars['node']->type;
if ($type == 'housing' || $type == 'service') {
$vars['template_files'][] = 'node-listing';
}
Here I have created a template called node-listing.tpl.php, which was copied from my node.tpl.php, then altered to meet the needs of the content type. So the preprocess_node function sees that the node type is one of these two types of content then uses the node-listing.tpl.php to render.
Advantage as I see it is that my node.tpl.php remains standardized, and as new content is created I have it to fall back on, always.
Only difference is... I don't want terms in my 'housing' content type... O man, guess I need two template files... Nope!
IF NODE IS!!
//Terms Not on Housing
if ($type == 'housing') {
$vars['terms']=NULL;
}So if the node is 'housing' I unset the terms variable, which means nothing is there. Same template, similar layout, design, and structure. One place to edit two content types that are similar. And a few lines of code to make it all possible.
The ability to use preprocess_node, and take action based on the node type can give themers some amazing power and control. Anyone else have some cool preprocess magic they want to share?
Comments
function
function wim_preprocess_node(&$variables) { $function = 'wim_preprocess_node'.'_'. $variables['node']->type; if (function_exists($function)) { $function(&$variables, $hook); } }Each nodetype gets its own preprocess_node function like this, so the code can be a bit cleaner.What's wrong with multiple
Post new comment