We have found Chapter Three to be a valuable resource in both training and development of custom Drupal functionality. In addition, getting Chapter Three’s help with scaling Drupal has been a real asset.
Jim Nisbet, CTO Highwire Press
With node template files you are often limited to something like node.tpl.php and node-blog.tpl.php. Often times it'd be nice to make a different template for just one specific node or a different template for teaser/list view and full node view.
Using the PHP code below, Drupal will look for these template files, split by page or no page view. This gives more fine grained control over your node tpl.php files.
1) node-[nid]-page.tpl.php
Node by itself on a page, specific NID
2) node-[type]-page.tpl.php
Node by itself on a page, specific type
3) node-default-page.tpl.php
Node by itself on a page, default
4) node-[nid].tpl.php
Node in list/teaser, specific NID
5) node-[type].tpl.php
Node in list/teaser, specific type
6) node.tpl.php
Node in list/teaser, default.
*UPDATE* Earl Miles has shown this can be done much simpler than what I had previously. Revised code below.
Place this code in your template.php file in your theme's directory.
<?php
function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {
case 'node':
// Here is the way to switch to a different node-<something> template based on node properties.
if ($vars['page']) {
// This is LIFO (Last In First Out) so put them in reverse order, i.e
// most important last.
$vars['template_files'] = array('node-default-page', 'node-'. $vars['node']->type .'-page', 'node-'. $vars['node']->nid .'-page');
}
else {
$vars['template_files'] = array('node-'. $vars['node']->nid);
}
break;
}
return $vars;
}
?>Thanks Earl! I didn't know $vars['template_files'] was available for the node case. I've updated the main post.
I also changed the fallback for the page view to node-default-page.tpl.php since node-page.tpl.php overlaps with the "page" node style.
Nice explanation.
Very similar things can be done with blocks, regions ...
See http://drupal.org/node/104319
hi ,
I used the above function phptemplate_preprocess_page for my drupal6 site and found that the .template page runs and when i print
print ' ghfghgf';
print htmlspecialchars(print_r(get_defined_vars(), TRUE), ENT_QUOTES);
print ''; in .tpl.php page
,the variables i added did not appear in my html page ,
is it because the function is not overwriting the default variables or am i using the wrong variables
please guide me
Thank you.
Here's the Drupal 6 version, with a preprocess function:
/**
* Override or insert variables into the node templates.
*
* @param $vars
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("node" in this case.)
*/
function pokeronline_preprocess_node(&$vars, $hook) {
switch ($hook) {
case 'node':
// Here is the way to switch to a different node- template based on node properties.
if ($vars['page']) {
// This is LIFO (Last In First Out) so put them in reverse order, i.e
// most important last.
$vars['template_files'] = array('node-default-page', 'node-'. $vars['node']->type .'-page', 'node-'. $vars['node']->nid .'-page');
}
else {
$vars['template_files'] = array('node-'. $vars['node']->nid);
}
break;
}
}The code posted for Drupal 6 destroyed my Views (view module) templates since it creates the vars['template_files'] from scratch. I changed the code to just append these new template files. This is a little untested, so use it at your own risk.
switch ($hook) {
case 'node':
// Here is the way to switch to a different node- template based on node properties.
if ($vars['page']) {
// This is LIFO (Last In First Out) so put them in reverse order, i.e
// most important last.
$vars['template_files'] = array_merge($vars['template_files'], array('node-default-page', 'node-'. $vars['node']->type .'-page', 'node-'. $vars['node']->nid .'-page'));
}
else {
$vars['template_files'] = array_merge($vars['template_files'], array('node-'. $vars['node']->nid));
}
break;
}
You have not mentioned that these template files are for drupal 5 not for drupal 6.
http://www.firstearthbattalion.org/?q=node/30275 Soma and soma tablet EMILIO
http://www.firstearthbattalion.org/?q=node/30276 Soma and soma express prepare
http://sdeklover.webgarden.com/ Viagra and viagra online no prescription overnight selalu
http://wfnx.com/members/order-xanax-overseas.aspx order xanax overseas and order xanax overseas
http://www.jamespot.com/a/1875558-buy-xanax-bars-next-day-shipping.html xanax and no prescription xanax castro
http://www.jamespot.com/a/1875591-Valium-next-day-delivery-buy-cash-deli... Valium and cheapest valium LESSON
http://www.firstearthbattalion.org/?q=node/23277 Tramadol and tramadol NASIONAL
http://www.firstearthbattalion.org/?q=node/23380 Soma and order chead soma emailed
http://vukoletas.webgarden.com/ Tramadol and cod tramadol commentaire
http://www.jamespot.com/a/1875547-buy-soma-next-day-cod.html soma and soma prescriptions decent
http://www.firstearthbattalion.org/?q=node/30272 Tramadol and tramadol cheap fedex weickseldscc
http://www.jamespot.com/a/1875602-buy-cheap-online-pharmacy-ultram.html ultram and online pharmacy ultram Louis
http://www.firstearthbattalion.org/?q=node/30271 Tramadol and tramadol prescription drug LEARNING
http://www.jamespot.com/a/1875627-Pharmacy-Tramadol-No-Prescrption.html Tramadol and tramadol online contreraslackland
http://www.jamespot.com/a/1875635-Tramadol-For-Sale-low-price.html Tramadol and tramadol 24hour barrientesrobins
http://sendindas.webgarden.com/ Tramadol and tramadol prescription drug sinchon
http://www.jamespot.com/a/1875568-SALE-where-buy-generic-viagra-online-n... viagra and buy generic viagra pills menyatakan
http://derevalen.webgarden.com/ xanax and xanax in US Graduation
http://www.firstearthbattalion.org/?q=node/23389 Ativan and ativan cheap overnight Average
http://www.firstearthbattalion.org/?q=node/23410 Diazepam and cheap online order diazepam newspapers
http://www.jamespot.com/a/1875242-Tramadol-Overnight-No-Prescription-No-... Tramadol and overnight tramadol cod Software
http://lastalkon.webgarden.com/ Soma and order soma superimposed
http://sonleras.webgarden.com/ Tramadol and next day tramadol instrumental
http://www.firstearthbattalion.org/?q=node/30273 Tramadol and tramadol cod orders reviewing
http://www.jamespot.com/a/1875576-Order-Ambien-no-prescription-online-on... Ambien and us ambien breaches
[url=http://60idnpen0y2btu7a.com/]61hp0gw38wnyme6z[/url]
[link=http://xssmvw5dcq22e677.com/]b2i9m79p2sw4d5j1[/link]
ebetqn9ahzqoyai2
http://ct36xj29692yrhp4.com/
[url=http://60idnpen0y2btu7a.com/]61hp0gw38wnyme6z[/url]
[link=http://xssmvw5dcq22e677.com/]b2i9m79p2sw4d5j1[/link]
ebetqn9ahzqoyai2
http://ct36xj29692yrhp4.com/
We have found Chapter Three to be a valuable resource in both training and development of custom Drupal functionality. In addition, getting Chapter Three’s help with scaling Drupal has been a real asset.
Jim Nisbet, CTO Highwire Press
While I haven't tested this directly, something along these lines should do the trick just as well and be even easier.
<?php
function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {
case 'node':
// Here is the way to switch to a different node-<something> template based on node properties.
if ($vars['page']) {
// This is LIFO (Last In First Out) so put them in reverse order, i.e
// most important last.
vars['template_files'] = array('node-page', 'node-'. $vars['node']->type .'-page', 'node-'. $vars['node']->nid .'-page');
}
else {
vars['template_files'] = array('node-'. $vars['node']->nid);
}
break;
}
return $vars;
}
?>