I want to have every two articles on the homepage within a div structure.

However, I can't get this done in my node.tpl.php file: if I use the zebra variable to print a tag before the node when zebra is odd, and a closing tag after the node when zebra is even then this only works when there are an even number of articles, or otherwise the last div won't be closed.

In code:

<?php if($zebra=='odd'): ?>
     <div class='row'>     
 <?php endif; ?>

 <div id="node-<?php print $node->nid; ?>" class="[..]">
 [rest of node here]
 </div><!-- /#node-x -->

 <?php if($zebra == 'even'): ?>
     </div><!-- /.row -->
 <?php endif; ?>

To make this work, I must be able to know when a node is the last one. But there isn't a variable $last in the node template file, neither is there a $count variable that hold the total number of nodes.

Does anyone has done something similar before?


The node template is not (usually) aware of the context in which it's been called; when you list nodes as teasers each node is themed individually, and has no awareness of the other nodes in the list.

What you need to do is find out what template/function is responsible for outputting the list of nodes in question and override that in your own theme/module to add the extra divs.

The Theme Developer module might come in quite handy here, it provides an in-page way of isolating which theme functions have been responsible for rendering which areas of the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.