$title Variable Is Empty In page.tpl.php

KarlKedrovsky - June 2, 2008 - 22:35

I'm having a hard time understanding how the $title variable is set in phptemplate. I've created a new theme, using zen as a starting point, and the $title variable is unset/empty in page.tpl.php but is set as expected in node-xxx.tpl.php. I've googled until my brain is numb but I can't find anything that would explain this. From what I can see I would expect to see $title in page.tpl.php just like I do in node-xxx.tpl.php.

I dug through the phptemplate code and found that the title variable is set by calling drupal_get_title() so I added the following in both page.tpl.php and node.tpl.php:

<?php
print drupal_get_title();
?>

I get nothing from page.tpl.php and the expected title in node-xxx.tpl.php. If I switch to the zen theme I see the title displayed by page.tpl.php just fine.

Can anyone tell me what I'm doing wrong here? I'm not sure what, if any, additional information would be helpful so if I can provide anything that might be helpful please let me know.

$title

roopletheme - June 3, 2008 - 00:41

I also struggled with this... here's what I think I've come to understand... hope it helps.

I think you'll find that $title is valid in page.tpl.php for 'full node pages' (i.e. yoursite/node/6), but that it will be empty for node lists (like the default front page.) For full node pages, $title will match the node title. For other pages (yoursite/admin, yoursite/user), it's usually set by the module providing the page using drupal_set_title.

You'll also see a value for $title in node-xxx.tpl.php, which should always match the node title (unless you're using something like the page title module.)

Two places in a theme that you'll typically see $title displayed: page.tpl.php and node.tpl.php. In Garland (5.7), you'll find this code in page.tpl.php:

<?php if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>

This code will print a title for almost every page on your site... but it will display nothing on the default front page because $title will be empty.

Then there's this code in node.tpl.php:

<?php if ($page == 0): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>

This code will display the node title ($title in this context) for a node, but only on the front page ($page == 0).

To summarize, $title will have a value in page.tpl.php for pretty much every page of your site EXCEPT for the front page. This value will be displayed by the code in page.tpl.php. On the front page of your site, $title will be blank, and the individual node titles will be displayed by the code in node.tpl.php.

$title Still Missing In page.tpl.php

KarlKedrovsky - June 3, 2008 - 13:41

Thanks for the excellent explanation, but I'm seeing an empty $title even on the full node pages. Just to test and illustrate the problem I created a bare bones test theme that contains the following page.tpl.php:

<html>
<head>
  <title>Test</title>
</head>
<body>
  <h1>Title from page.tpl.php: <?php print $title; ?></h1>
  <p><?php print $content; ?></p>
</body>
</html>

And the following node.tpl.php:

<h1>Title from node.tpl.php: <?php print $title; ?></h1>

When I visit /node/1 (a "page" node that has a title of "Home Page") I get the following:

Title from page.tpl.php:

Title from node.tpl.php: Home Page

If I switch to the zen theme and visit the same node the title displays just fine. As you can imagine I'm somewhat confused but since the zen theme works I can't help but think that I'm just missing something simple here.

Fixed

KarlKedrovsky - June 3, 2008 - 18:40

Given the response from roopletheme I figured the problem was with my install since I wasn't seeing the normal behavior. A tedious process of elimination led me to the conclusion that it was the version of the panels module that I was using. An upgrade to the latest beta (4b) fixed the problem.

Update: It's not really fixed, I had forgotten to run update.php. Prior to running update.php the titles would show up but afterward they would not. I've narrowed the issue down to the Views Pane bit of Panels as disabling this feature "solves" the problem.

Looking for an update

mgifford - January 2, 2009 - 21:52

I'm curious if you've figured this out or not. roopletheme's suggestion should have worked. I also liked his example where he checked for $title before printing it to the browser. It produces better output for accessibility.

If switching themes is all that is different between the version that worked and the version that didn't, then it's not likely associated with your choice of modules. I'd be interested in hearing what resolved this or if it is still an outstanding issue.
--
OpenConcept | CLF 2.0 | Tech | Screencasts

Possible explanation for similar problem with Drupal 6 and Views

mmatsoo - April 8, 2009 - 07:45

Though I'm using Drupal 6, this may be of use. My $title variable was coming up blank on one of my nodes and I tracked it down to my inclusion of a view in the body of that node. (inserted with php using views_get_view function and $view->execute_display) In my view's basic settings I had left the view's title blank and this seemed to overwrite what would have been Drupal's regular $title value. (ie. the title of that node)

In short, I think my blank view title overwrote the existing node title making the $title variable an empty string.

Me too - now how do we get our titles back?

yakker - April 28, 2009 - 20:59

;)

Giving me headaches trying to figure this out. If I try to implement a views preprocess function in template.php, I can't seem to use the $vars['title'] = "foo" like I would be able to use with the page preprocess function. If anyone has any suggestions for how to make a single node VIEW assign the $node->title to the $title variable of the page.tpl.php file, it would be golden!

Problem with $tabs as well ...

zombiethread - June 7, 2009 - 02:43

Including a view block on a page also caused my $tabs variable to be empty. Disabling the view block on the brought it back with flying colors. Anyone ever find a fix for this? If not, I hope this helps because right now ... I'm stumped.

What works is loading the node again:

levavie - June 30, 2009 - 21:27

What works for me in Drupal 5 is loading the node again:

  <?php if ($page == 0): ?>
    <h2><a href="<?=$node_url; ?>"><?php $node= node_load ($node->nid); print ($node->title); ?></a></h2>
  <?php  endif; ?>

Amnon
-
Professional: Drupal Israel | Drupal Development & Consulting
Personal: Hitech Dolphin: Regain Simple Joy :)

I'm resorting to a similar

BeechyBoy - November 2, 2009 - 08:54

I'm resorting to a similar hack at the moment by loading the node again, has anyone discovered what's happening here?

Looking for a Drupal developers? http://www.fixdit.com
ashleydavison [at] fixdit.com

 
 

Drupal is a registered trademark of Dries Buytaert.