creating a node.tpl.php file
drupal | node.tpl.php | template | theming
node.tpl.php in box_grey
We looked at page.tpl.php in box_grey which is a great, simple theme. It does have some limitations though:
- Tables are still used for visual Layout
- Structural tags are used strictly for clearing: <br class="clear" />
- Some static title and alt values are explicitly set
The default node.tpl.php mark-up on the other hand is quite usable. It's not nearly as complex as far as structure goes because it does not require any mark-up for layout. It is just the content after all:
node.tpl.php default code
<div class="node<?php print ($sticky) ? " sticky" : ""; ?>">
<?php if ($page == 0): ?>
<h2><a href="/<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>
<?php print $picture ?>
<div class="info"><?php print $submitted ?><span class="terms"><?php print $terms ?></span></div>
<div class="content">
<?php print $content ?>
</div>
<?php if ($links): ?>
<?php if ($picture): ?>
<br class='clear' />
<?php endif; ?>
<div class="links"><?php print $links ?></div>
<?php endif; ?>
</div>
There is still one small issue with the code above:
- Structural tags are used strictly for clearing: <br class="clear" /> - it would be best to remove this tag
node.tpl.php modified code
<div class="node<?php print ($sticky) ? " sticky" : ""; ?>">
<?php if ($page == 0): ?>
<h2><a href="/<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>
<?php print $picture ?>
<div class="info"><?php print $submitted ?><span class="terms"><?php print $terms ?></span></div>
<div class="content">
<?php print $content ?>
</div>
<?php if ($links): ?>
<?php if ($picture): ?>
<?php endif; ?>
<div class="links"><?php print $links ?></div>
<?php endif; ?>
</div>
The <br /> tag has been removed from the above code.
Creating a node.tpl.php document
- Create a new plain text document in the theme folder you created. Name this file node.tpl.php
drupal/themes/my_theme/node.tpl.php
- Copy the "node.tpl.php modified code" above and paste it into your node.tpl.php and save your document
- Examine the available variables for node.tpl.php
- Use the appropriate syntax to apply any additional node.tpl.php variables

Recent comments