Drupal: Variables of node.tpl.php file
In this article I propose you to clarify the variables of a page template (page.tpl.php). This is necessary to create a website design according your needs.
Drupal 6
The file is responsible for displaying content in full view or in the lists.
Basic variables:
- $title: Title of the node (no HTML tags).
- $content: Content of the node. Is it a full content or just a teaser depends on a state of $teaser variable.
- $picture: Avatar or user picture from theme_user_picture() function.
- $date: Formatted date of creation (If you wish to format it with different function (for example, format_date()), use $created variable.
- $links: Links like “Read more”, “Leave a comment”, etc. received from theme_links() function.
- $name: Formatted author’s name received from theme_user() function.
- $node_url: A full path to a page with the node.
- $terms: Formatted list of taxonomy terms received from theme_links() function.
- $submitted: formatted information about the author and time of the node creation received from theme_node_submitted() function.
Additional variables:
- $node: Full object of the node. Might contain unsecure data.
- $type: Type of the node, e.g. story, page, blog etc.
- $comment_count: A number of comments added to the node.
- $uid: Identifier of the node’s author.
- $created: Time of the node creation in format Unix timestamp.
- $zebra: Outputs values “even” or “odd” which shows the position of the node in the lists.
- $id: Position (order number) of the node in the lists.
Status variables:
- $teaser: TRUE for teaser mode.
- $page: TRUE for full view mode.
- $promote: TRUE for “Show on the main” status.
- $sticky: TRUE for “Fix above the lists” status.
- $status: TRUE for “Published” status.
- $comment: Comments settings for the node: Disabled – 0. Read only – 1. Read/Write – 2.
- $readmore: TRUE if a teaser doesn’t include the full content.
- $is_front: TRUE for the home page.
- $logged_in: TRUE for the authorized user.
- $is_admin: TRUE if current user is administrator.
Drupal 5 (and earlier)
This template is used to show nodes on the websites as in full view as well in the lists. It can effect on $content variable output.
Available variables
$content – Full content of the node, the teaser in the list mode.
$date – Formatted date of the node creation.
$directory – Full path to a current theme folder, e.g. themes/garland or themes/garland/minelli.
$id – Node ID.
$is_front – TRUE if current page is the home page.
$links – List of the links of the current node.
$main (4.6) – TRUE if the node appears in the list. This variable was rarely used in version 4.7 and higher.
$name – Formatted author’s name with the link.
$node (Object) – Node object. To view all properties of a current $node object, insert the following code to your node.tpl.php file: print_r($node);
$node_url – Path to a page with the node.
$page – TRUE if the node is shown on its page.
$picture – HTML-code of user picture (if enabled).
$sticky – TRUE if status of the node is «Fix above the lists».
$submitted – HTML-code of Author and creation date if showing of this info is enabled to this kind of nodes.
$taxonomy (Массив) – A set of HTML-code of links to taxonomy terms of this node.
$teaser – TRUE if the node isn’t shown in the form of a teaser
$terms – HTML-code of all links to taxonomy terms of the given node.
$title – Node title.
$zebra – Value showing if the node has even or odd number in the list.
Hints:
How to print the list of available variables?
Output of variables array
print ‘<pre>';
print_r(get_defined_vars());
print ‘</pre>';
Output of 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>