Making additional variables available to your templates
Examples from this forum discussion. The $hook refers to the area the variable is to be used in (e.g. for comment.tpl.php, it would be "comment").
This function needs to be defined in a template.php file, which is placed inside the template directory (for instance : themes/box_cleanslate/template.php)
<?php
function _phptemplate_variables($hook, $vars) {
switch($hook) {
case 'comment' :
$vars['newvar'] = 'new variable';
$vars['title'] = 'new title';
break;
}
return $vars;
}
?>The output of this function is merged with the variables returned from phptemplate_comment, so you can easily adjust whichever variables you feel necessary.
Your comment.tpl.php file will now have a new variable available in it
called $newvar. Similarly the $title variable will be overridden with the value specified in
the function.
A neat trick is to count how many times each of the hooks is called, so you can pass an extra variable. re :
<?php
function _phptemplate_variables($hook, $vars) {
static $count;
$count = is_array($count) ? $count : array();
$count[$hook] = is_int($count[$hook]) ? $count[$hook] : 1;
$vars['zebra'] = ($count[$hook] % 2) ?'odd' : 'even';
$vars['seqid'] = $count[$hook]++;
return $vars;
}
?>That is 'even' if it is an even number, and 'odd' if it is odd. This means you do zebra striping (ie: alternating colors) for each of your nodes / blocks / comments / whatever.
Then you can set up a some styles for class='$zebra' , which handle the alternating colors.
Another example is a flag to show us if we are looking at a node. Might be handy for rendering items different, when someone is looking at an article.
<?php
function _phptemplate_variables($hook, $vars) {
switch ($hook) {
case 'page':
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
$vars['content_is_node'] = TRUE;
}
break;
}
return $vars;
}
?>Note that the switch is kind of obsolete here, but i leave it here, because you might want to add more variables. In that case you need them.
The args() checks will see if you have an url like /node/NID/ and not like /node/NID/edit or /node. If that is found, we set the flag TRUE.

Also valid for Smarty Theme Engine
Just want to drop a line that this code is valid for the Smarty Theme Engine as well.
Just replace
'_phptemplate_variables' with '_smarty_variables',
template.php with smartytemplate.php,
.tpl.php with .tpl,
and all that ugly phptemplate code with beautiful smarty =)
Getting node object into comment.tpl.php
Okay I finally figured out how to do this (in Drupal 4.7 at least). Like I said in an above comment I wanted to be able to use the node object in my comment.tpl.php theme file. This allows me to style the comments differently depending on what type of content page you are looking at. They look like this on a blog entry and like this on a Q&A page where authorized users provide "answers".
My template.php file looks like this:
<?phpfunction _phptemplate_variables($hook, $vars) {
switch($hook) {
case 'comment' : // PORTS THE $NODE OBJECT TO THE COMMENT TEMPLATE
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$vars['node'] = node_load(arg(1));
}
break;
}
return $vars;
}
?>
The node ID (nid) is gathered from the URL with the arg function and then the node object is loaded up using this information in the noad_load function.
Then I can use this info in my comment.tpl.php file
<?phpif ($node->type == 'flexinode-6') {
echo 'special comment format';
} else {
echo 'regular comment format';
}
?>
So glad I was able to finally get this one working.
Loading node object directly in comment.tpl.php
Here's a similar approach that works in Drupal 5.6 (unlike the above) and doesn't require running node_load() over and over for every comment on your page... put these lines in your comment.tpl.php file:
<?phpglobal $node;
if ((!$node) && (arg(0) == 'node') && (is_numeric(arg(1)))) {
$node = node_load(arg(1));
}
?>
What is the meaning of loading node object directly?
I don't understand what u mean by loading node object directly. I am working on localhost on my website EggDir and want to drupalify it asap, kindly clarify the node loading, sorry if im bothering, im just new to the drupal code and don't understand the inner coding properly.
Thanks
Sohrab Khan
-----------------------
SohRab | 3Tons | RabDir
clarification
Sorry for the long response time, Sohrab. With any luck you've solved your problem by now, or upgraded to Drupal 6! What I meant by loading the node "directly" in the tpl.php file is that the code I provided affects only one file -- the file where you want the change to appear -- rather than two files (the other being template.php). It may not be the correct Drupal approach to theming, but it works and is easier to understand, to my way of thinking.
Drupal 6
In Drupal6, _phptemplate_variables() has been deprecated in favor of Theme Preprocessors ... which basically take care of that "switch" function for you, and cache things.
.dan.