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.

Comments

marcvangend’s picture

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

daemon’s picture

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.

 // 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.
 }
nevets’s picture

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

daemon’s picture

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

nevets’s picture

What if you try adding it in hook_preprocess_page()?

daemon’s picture

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):

 //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)

 // 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:

//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
matt2000’s picture

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.,

$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.

matt2000’s picture

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