Node theming by field: deconstructing $node->content in Drupal 5
The CCK ‘display fields’ settings are very useful for theming nodes by content type, but I often find myself having to get further into customizing node output field by field.
Once I decide there’s no way getting around taking finer control of my node’s fields, I create a node-{type}.tpl.php by copying my existing node.tpl.php and open up Devel’s ‘Node Render’ tab. Then I delete the $content variable from the template and start adding things like
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">print </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">content</span><span style="color: #007700">[</span><span style="color: #DD0000">'field_image'</span><span style="color: #007700">][</span><span style="color: #DD0000">'#value'</span><span style="color: #007700">]; <br></span><span style="color: #0000BB">?></span></span>
and <span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">print </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">content</span><span style="color: #007700">[</span><span style="color: #DD0000">'body'</span><span style="color: #007700">][</span><span style="color: #DD0000">'#value'</span><span style="color: #007700">]; <br></span><span style="color: #0000BB">?></span></span>
The $content variable is nice for amateurs, but we need the unrendered $node->content to get good control of a node.
While this gives me complete control over the output of the node’s fields, the drawback is that now if I add a new field to the node, or if I add a module that adds something to $node->content, I have to go back to my template and add in this new element. Because I often do my theming and development in parallel, this can be rather annoying, and there is also a danger that I could overlook doing it.
Therefore I think it may be more practical to use code like this in the node template: