Render and Hide in Drupal 7 templates

Among the many changes to Drupal 7 that theme developers should be aware of. Is the inclusion of the render() and hide() API functions available in the node and user templates.

Quick explanation – Granular control & Forward compatibility

Basically these two functions give you finer granular control over printing node content while at the same time allowing your template to stay up to date with new node additions (maybe added by a new CCK field or module).

A simple use case

If you have ever been required to create a custom node.tpl.php template it is most likely the default method for displaying content “printing it all from top to bottom in order of weight”

<?php print $content ?>

does not meet your layout needs.

You may want to:

Outcome 1
Put some content on the right, show some content like links in more than one place. Or in other words you may want full control of where everything is placed

Outcome 2
Or you may want to put one content piece on the right and simply just display everything else.

Possible solutions

The common approach to Outcome 1 with Drupal 6 is to manually theme each element by accessing each one using

 $node;

like so;

<div class=’right’>  <?php print $node->field_myfield; ?> </div>

The problem here is when you want to approach Outcome 2. In Drupal 6 you can print all the content in a node at once by doing something like this;

 print $content; 

But if you wanted to print an element from $content first and then print all the rest before without printing the element you printed before again you cannot simply do something like the example below;

//show field_myfield first
<div class=’right’>  <?php print $node->field_myfield; ?> </div>

//field_myfield is part of $content so it will show up again. 
<?php print $content; ?>

In order to achieve Outcome 2 you will need to use the same method for as used for Outcome 1. This is all fine except that it makes for a hell of a lot of typing if you have a lot of content elements in $content.

Drupal 7 solution

By using the render() and hide() in Drupal 7 you can achieve outcome 2 by the doing the following

hide($content[‘field_myfield’]);

print render($content);

<div class=’right’>  

<?php print render($content[‘field_myfield’]); ?> 
</div>
print render($content);

The hide() function will stop the $field_myfield content piece from being printed when you print all the content using render($content) then you can then later print $field_myfield later on using render($content[‘field_myfield’]).

As well as the advantages demonstrated in the example here using render() and hide() over manually dealing with node content means that new node additions will not be missed when you use print render($content).

Further reading

To find out more I suggest checking out the original issue here . I enjoyed reading through this issue because it is also a good demonstration of the refactoring process

To find out about other changes to theming in Drupal 7 check out http://drupal.org/node/254940

Also have a look at the show() function in the drupal api page

One thought on “Render and Hide in Drupal 7 templates

Leave a comment