theme pages

Get nodes as an array for greater flexibility

Last modified: August 27, 2009 - 00:34

By default, in your page.tpl.php you have $content which contains all the nodes and other things like pagination..etc. The snippet below tries to give more flexibility by breaking down $content, and breaking down nodes themselves into an array of nodes. Precisely, what happens is:

  • In page.tpl.php: You get a new array (YES!) called $nodes, contains the XHTML for each node.
  • In page.tpl.php: $content now does NOT contain the nodes. It contains some other things like pagination..etc

Be aware that this snippet doesn't really give you any difference if you didn't adjust your page.tpl.php to exploit the extra flexibility of having nodes as an array. Some examples of doing neat things with $nodes array can be found after the snippet below.

Here comes the PHP snippet, place this in your template.php:
<?php
function yourtheme_node($node, $teaser = 0, $page = 0) {
set_themed_node(phptemplate_node($node, $teaser, $page));
}

function set_themed_node($node = NULL) {
static $themed_nodes;
if ($node) {
$themed_nodes[] = $node;
}
else {
return $themed_nodes;
}
}

function _phptemplate_variables($hook, $vars) {
$myvars = array();
switch ($hook) {
case 'page':
$myvars['nodes'] = set_themed_node();
break;
}

Syndicate content
 
 

Drupal is a registered trademark of Dries Buytaert.