Anatomy of a Drupal Theme - The Node Object

The function behind the $node object is node_load(). This brings the $node object into play within your node.tpl.php template file and gives you the ability to display any variables from the node. Drupal handles everything behind the scenes -- access permissions, attached taxonomy, attached files, meta information, and more -- and hands you the gift wrapped object. In addition to the core variables, the node object also includes any added variables, such as CCK fields. 

In other words, you can theme a completely custom page to match whatever you dreamed up in Photoshop simply by printing the needed $node values into the appropriate places. This is probably obvious to many, but the elegance can get lost on the new user. For example, take this documentation page from Drupal.org:

For the sake of this article, let's assume there are a few standard node fields and a few additional CCK fields within this page:

While the sidebars and overall layout are defined in our page.tpl.php, the node.tpl.php file could be as simple as this:

<h1><?php print $node->title; ?></h1>
<div class="meta"><?php print $node->author; ?> - <?php print format_date($node->created, 'custom', 'D M j Y'); ?></div>

<div class="terms">
   <?php  // print out each term attached to this node, link it to it's taxonomy id listing showing only the name of the term as the link text
   foreach ($node->taxonomy as $term) {
      print '<a href="' . base_path() . 'taxonomy/term/' . $term->tid . '">' . $term->name . '</a>';
   }
   ?>
</div>

<div class="content">
  <?php print $node->content['body']['#value']; ?>
  <?php print $node->field_module_requisites_value['0']['safe']; ?>
  <?php print $node->field_module_documentation_value['0']['safe']; ?>
</div>


We're grabbing the title by printing out $node->title, the author by printing out $node->author, etc. To get all of taxonomy terms we cycle through $node->taxonomy and print out the term name ($term->name) while linking to the term ID ($term->tid).

We're also printing out some CCK fields with $node->field_module_requisites_value and $node->field_module_documentation_value. The ['0']['safe'] are sub arrays that include specific types of data within those CCK fields, but they shouldn't confuse you!

Where do we get all of the $node variable names? The best way to see them in their full hierarchical glory is to create a new node template file, such as node-blog.tpl.php, and add this code into the bottom of the file:

<pre>
<?php print_r($node); ?>
</pre>


You'll get a nicely formatted, easy to read teardown of your complete node object.
Understand and use the $node object and you'll have complete pixel-perfect control over your end themes.

For a complete list of core $node variables, check out the Drupal 6 Node Object Reference chart.

The next post on my tear-down of Drupal themes will focus on extending your theme via proper includes (CSS, JS, etc.) and using jQuery within the Drupal environment. 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.