Drupal 6 Hook Variables in template.php

webadmin's picture
Coding

This is example code snippet when you hook / modifying a variable in page, node or comment and do something else. ! just try it.

Install and enable devel modul for in use code dsm($vars);

<?php
function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {
case 'page':
// code relating to variables for page templates here
// EXAMPLE - is the current user logged in?
$vars['logged_in'] = ($user->uid > 0) ? TRUE : FALSE;
break;

case 'node':
// code relating to variables for node templates here
// EXAMPLE - on front page teasers, make a comment count var using a query
if ($vars['teaser'] && $vars['is_front']) {
$vars['comment_count'] = db_result(db_query("SELECT comment_count
from {node_comment_statistics} where nid = %d", $node->nid));
}

break;

case 'comment':
// code relating to variables for comment templates here
break;
}
// anything that you require for all the above, put outside the switch statement
return $vars; // this passes the variables back to the relevant template
}
?>