template.php does not have access to standard variables
gollyg - April 15, 2006 - 11:27
| Project: | PHPTemplate |
| Version: | HEAD |
| Component: | Template Defaults |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
I may be doing something wrong, but I cannot pass basic variables from the a template.php file. The additional variables such as zebra are available, but the majority of variables, eg node->title etc arent available. I have tested by adding
<?php
print_r(get_defined_vars());
?>but other than the custom variables nothing shows up.
Seems to be affecting some others - http://drupal.org/node/25297.

#1
Bumped this up to critical as it would seem to be a serious problem with the engine (or i have got it completely wrong ;).
#2
this is not critical, and is by design
template.php itself has access to nothing. drupal doesn't have any context, or global variables. The only exception to this rule is the currently logged in global $user.
If you mean the _phptemplate_variables function it gets called with variables in it's $variables array parameter, and the contents of that array is different depending on which theme function is calling it.
The only time a $node object is passed through phptemplate_variables, is if ($hook == 'node') , or if ($hook == 'page') && (page is a node page), and then it is accessed as $variables['node'];
There is a lot of documentation on the phptemplate handbook about how to use this function
http://drupal.org/phptemplate
#3
making this active again.
looking at the link, perhaps you did not explain the bug properly.
#4
also
lines 212 to 214 of phptemplate.engine, that handle the node passing, have not changed much
http://cvs.drupal.org/viewcvs/drupal/drupal/themes/engines/phptemplate/p...
#5
Eek! Re-reading my post i realise i described it terribly!
#6
I am having this same problem so maybe I can help explain:
I need the node object (well, the nid part at least) in my page.tpl.php file and so am using this code in the template.php file. It used to work in 4.6 but is giving me a php error in 4.7:
<?phpfunction _phptemplate_variables($hook, $vars) {
switch($hook) {
case 'page' :
$vars['nid'] = $vars['node']->nid
break;
}
return $vars;
}
?>
Has there been a change that stops this from working? If so we need to change the documentation.
#7
The node object is passed to page.tpl.php when the url matches the format 'node/nid', then and only then.
The node object is not available on any other pages, at any other time.
You do not need to alias the 'nid' variable, as you can access the $node->nid variable directly from within your page.tpl.php.
The only thing that has changed about this code since it was originally checked into the drupal core repository, is that we
got rid of the node_load(array('nid' => nid) ) formatting, and now use node_load(nid).
#8
This restriction is a problem for me as I have a skin that requires a sidebar with information about the node owner. I need to know who the node owner is in template.php to do this properly.
Is there a way to route around this problem without hacking the Drupal core code? Basically I need access to $node->uid and $node->name from template.php for all nodes.
#9
Same here, I would like access to this info. Seems really basic, especially when everything disappears within context of a node.
#10
<?phpif ((arg(0) == 'node') && is_numeric(arg(1)) ) {
$node = node_load(arg(1));
}
?>
Once again, this is not a bug. this is how Drupal (not phptemplate was designed), to fix this in any meaningful way without a workaround would involve modification of the Drupal API to store the things it retrieves.
#11
perhaps if folks here posted some of the code in their template.php file we could see what else we could do on this.
#12
Subscribing. Here's the code that works fine for me in node.tpl but not in template.php.
foreach( (array)$node->taxonomy as $term ) {if ($term->tid == 176) {$banner_switch = "yes";}
}
And here it is again when inside my template.php....
function phptemplate_preprocess_node(&$vars, $hook) {
$function = 'phptemplate_preprocess_node'.'_'. $vars['node']->type;
if (function_exists($function)) {
$function(&$vars);
}
return $vars;
}
function phptemplate_preprocess_node_newsletter(&$vars) {
foreach( (array)$node->taxonomy as $term ) {
if ($term->tid == 176) {$banner_switch = "yes";}
}
}
My phptemplate_preprocess_node_newsletter function is working fine. I did the below to test to make sure I could assign a variable and print it out in my node.tpl....
function phptemplate_preprocess_node_newsletter(&$vars) {$vars['testVar'] = 'hi';
}