How to pass variable from node.tpl.php to page.tpl.php

daemon - July 6, 2009 - 19:57

I would like to pass a custom variable declared and set from node.tpl.php to be used in page.tpl.php, how can this be done? I have been searching all the documentation and forum posts already.

I think that depends on the

marcvangend - July 6, 2009 - 20:18

I think that depends on the variable, where it comes from, how it was calculated or generated... Can you be more specific?

I am working on a theme hack

daemon - July 6, 2009 - 20:52

I am working on a theme hack to allow comments and the comment form to placed anywhere in page.tpl.php, however, in order for things to function without break all the JavaScript I have to call a drupal_get_form function inside node.tpl.php or under template_preprocess_node.

Here is a snippet of the code in question and the variable I need to have passed to page.tpl.php.

<?php
// in node.tpl.php

if ($page == true && $node->comment == 2) {
   
// Triggers the drupal_get_form for the JavaScript to be loaded.
   
$node_comment_form = drupal_get_form('comment_form',array('nid' => $node->nid));
   
// Todo, pass $node_comment_form variable to page.tpl.php to avoid having to render the form twice.
}
?>

Why can't you call that in

nevets - July 6, 2009 - 20:53

Why can't you call that in page.tpl.php?

Still figuring out how it

daemon - July 6, 2009 - 21:04

Still figuring out how it works to be honest, it does not have the intended effect when I call it from page.tpl.php.

What if you try adding it in

nevets - July 6, 2009 - 21:28

What if you try adding it in hook_preprocess_page()?

It doesn't trigger what I

daemon - July 7, 2009 - 03:21

It doesn't trigger what I need.

To explain, I'm turning disabling the comments in my node through the following (or the equivalent in the preprocess):

<?php
//in node.tpl.php

// Remove comment display appended to $content by disabling the node's comment variable
$node->comment = 0;
?>
Then I have to re-create the form on page.tpl.php (in the preprocess in this case)
<?php
// in template_preprocess_page

   
if (module_exists('comment') && isset($vars['node'])) {
       
$vars['comments'] = comment_render($vars['node']);
       
$vars['comment_form'] = drupal_get_form('comment_form',array('nid' => $vars['node']->nid));
    }
?>

However, in doing so, all the JavaScript related to the form (Live Module JS, Comment-Notify Module JS, Textarea Resize JS, etc...) no longer appears. Through trial and error I noticed that if I run the following in node.tpl.php:

<?php
//in node.tpl.php

// We only need the JavaScript on the full pages and if the node originally allowed commenting
if ($page == true && $node->comment == 2) {
   
// Triggers the drupal_get_form for the JavaScript to be loaded
   
$node_comment_form = drupal_get_form('comment_form',array('nid' => $node->nid));
   
// Todo, pass $node_comment_form variable to page.tpl.php to avoid having to render the form twice.
}
?>
However, the side effect is that the form are
  • actually rendered twice, minor performance issue
  • the displayed form is a duplicate with "-1" appended to its ID ("edit-form-1" and "comment-form-1"), which may cause errors with some JS that hard codes them

By the time page.tpl.php is

matt2000 - October 22, 2009 - 17:20

By the time page.tpl.php is processed, the header section variables (e.g., $scripts) are already set.

I think you need to act before the theme layer is invoked at all; hence a custom module.

Implement hook_nodeapi('view') and the render the comment form as a property of the $node object. ($node->comment_form) Then the form will be available to you in page.tpl.php as $vars['node']->comment_form .

Or else, you may be able to force $scripts to be re-populated in theme_preprocess_page after you've rendered the comment form there, e.g.,

<?php
$vars
['comment_form'] = drupal_get_form(blah,blah);
$vars['scripts'] = drupal_get_js();
?>

But this is probably less efficient, since the scripts will be collected twice.

BTW, I just figured out that

matt2000 - October 22, 2009 - 18:26

BTW, I just figured out that Drupal 7 will fix this issue by adding $scripts , etc, later in the theme life-cycle.

 
 

Drupal is a registered trademark of Dries Buytaert.