Drupal: variables of node.tpl.php
Sometimes you need to work with template of a page in your site (node.tpl.php) file to adjust design of your site. In this article, I would tell you about node.tpl.php variables. I will tell about main variables, additional variables and status variables for Drupal 6 and Drupal 5.
Drupal 6
The file isn’t responsible for how to show data (fully or in lists)
Main variables:
- $title: node title (without HTML tags).
- $content: node content. Status of $teaser variable shows is it a full content or a teaser.
- $picture: avatar or user picture from theme_user_picture() function.
- $date: formatted creation date (use $created variable if you want to change format with format_date() function).
- $links: links of a node “Read more”, “Add a comment”, etc from theme_links() function.
- $name: formatted author’s name from theme_user() function.
- $node_url: full path to a node page.
- $terms: formatted list of node taxonomy terms from theme_links() function.
- $submitted: formatted info about node author and creation date from theme_node_submitted() function.
Additional variables
- $node: full node object. Could contain dangerous data.
- $type: node type i.e. story, page, blog etc.
- $comment_count: number of comments to a node.
- $uid: ID of a node author.
- $created: creation date of a node in Unix timestamp format.
- $zebra: shows “even” or “odd” – position of a node in lists. It is used to alternate colors of neighboring nodes.
- $id: node position (order number) in lists.
Status variables
- $teaser: TRUE for a teaser mode.
- $page: TRUE for full display mode.
- $promote: TRUE for “show on home” status.
- $sticky: TRUE for “anchor above lists” status.
- $status: TRUE for “published” status.
- $comment: comments settings for a node: disabled – 0. read only – 1. read/write – 2.
- $readmore: TRUE if node teaser doesn’t include a full content.
- $is_front: TRUE for a home page.
- $logged_in: TRUE for authorized user.
- $is_admin: TRUE if current user is administrator.
Drupal 5
This template is used to show content of a site (full site content or in a form of a list). It could affect on $content variable output only.
Available variables
$content – full content, teaser in a form of a list.
$date – formatted date of a node creation.
$directory – full path to a folder of a current theme, for example, themes/garland or themes/garland/minelli.
$id – node ID.
$is_front – TRUE if current page is a home page.
$links – list of links of a current node.
$main (4.6) – TRUE if a node appears as a list similar to a home page with teasers only. The variable is shortly used in Drupal 4.7 or higher.
$name – formatted author’s name with a link.
$node (object) – object of a node. To view all properties of a current object ($node), insert ‘print_r($node’ code into node.tpl.php);
$node_url – path to a page of a current node.
$page – TRUE if a current node appears on its own page.
$picture – HTML-code of a user avatar (if enabled).
$sticky – TRUE if status of a node is «Sticky at top of lists».
$submitted – HTML-code of an author and creation date (if enabled for a current node).
$taxonomy (array) – array of HTML-code of links to a current node taxonomy terms.
$teaser – TRUE if a node appears as a teaser.
$terms – HTML-code of all links to a current node taxonomy terms.
$title – node title.
$zebra – value that shows even or odd number of a node in a list.
Notes
How to print a list of all available variables?
Print variables array
print ‘<pre>';
print_r(get_defined_vars());
print ‘</pre>';
Print variables array with HTML marking.
print ‘<pre>';
print htmlspecialchars(print_r(get_defined_vars(), TRUE), ENT_QUOTES);
print ‘</pre>';
Default template for Drupal 5.x
<div id=”node-<?php print $node->nid; ?>” class=”node<?php if ($sticky) { print ‘ sticky'; } ?><?php if (!$status) { print ‘ node-unpublished'; } ?> clear-block”>
<?php print $picture ?>
<?php if ($page == 0): ?>
<h2><a href=”<?php print $node_url ?>” title=”<?php print $title ?>”><?php print $title ?></a></h2>
<?php endif; ?>
<div class=”meta”>
<?php if ($submitted): ?>
<span class=”submitted”><?php print $submitted ?></span>
<?php endif; ?>
<?php if ($terms): ?>
<span class=”terms”><?php print $terms ?></span>
<?php endif;?>
</div>
<div class=”content”>
<?php print $content ?>
</div>
<?php
if ($links) {
print $links;
}
?>
</div>
Default template for Drupal 4.7
<div class=”node<?php if ($sticky) { print ” sticky”; } ?><?php if (!$status) { print ” node-unpublished”; } ?>”>
<?php if ($page == 0): ?>
<h2><a href=”<?php print $node_url ?>” title=”<?php print $title ?>”><?php print $title ?></a></h2>
<?php endif; ?>
<?php print $picture ?>
<div class=”info”><?php print $submitted ?><span class=”terms”><?php print $terms ?></span></div>
<div class=”content”>
<?php print $content ?>
</div>
<?php if ($links): ?>
<?php if ($picture): ?>
<br class=’clear’ />
<?php endif; ?>
<div class=”links”><?php print $links ?></div>
<?php endif; ?>
</div>