Last updated July 3, 2013. Created by joachim on March 11, 2006.
Edited by davidmac, SLIU, Jeff Burnz, kingandy. Log in to edit this page.

You can give a teaser its own unique look by using the following condition on any node.tpl.php file:

<?php
if ($teaser) {
 
// node is being displayed as a teaser
  // Anything here will show up when the teaser of the post is viewed in your taxonomies or front page
} else {
 
//all other cases
  //Anything here will show up when viewing your post at any other time, e.g. previews
}
?>

Or, if you want to use HTML within the conditions, for example to make two completely different layout within the template:

  <?php if ($teaser): ?>
    <!-- teaser template HTML here -->
  <?php else: ?>
    <!-- regular node view template HTML here -->
  <?php endif; ?>

$teaser vs $page

Note that the $page variable indicates whether a node is standing alone or not, which isn't necessarily the same as whether it's being shown as a teaser. There are legitimate situations where $page == 0 and it's not supposed to be a teaser - for example, when you preview a node. Additionally, $page and $teaser can be set directly by any code that displays nodes via the node_view() function, so it's wise to think about unexpected combinations.

If you wish to provide a third layout for when the node is being displayed as neither a teaser nor a page, try this:

<?php
if ($teaser) { //if node is being displayed as a teaser
  //Anything here will show up when the teaser of the post is viewed in your taxonomies or front page
} elseif ($page) { //if node is being displayed as a full node
  //Anything here will show up when viewing only your post
} else { //all other cases
  //Anything here will show up when viewing your post at any other time
}
?>



Additional Flexibility with Negatve Logic

There are circumstances where it is necessary to use negative conditional logic (common throughout PHP programming). For example, you could style node layouts to display certain variables when they are 'not teaser' using the following example code:

<?php if (!$teaser): ?>
   <span class="title"><h4><?php print t('Written By'); ?>:</h4></span>
     <div class="author clearfix">
           <?php print $user_picture; ?>
           <p><em><?php print $name; ?></em></p>
     </div>
<?php endif; ?>

Depending on the complexity of your theme and template files, an 'if/else' statement might not be what you need, this is where the conditional negative is practicable.

We can apply the same logic to many other variables when theming.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Hi,
Thanks for the code. I am using the IF statement and that is working great on my node-TYPE.tpl.php except for one thing: the title is showing twice when I am taken to the page of the node. Thanks.

Youssef

<?php if ($teaser): ?>
<h4><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h4>
<div id="phone-style"> <?php print $node->field_phone[0][view]; ?> </div>
<div class="column grid_4"> <?php if (user_access('rate content')) { print fivestar_widget_form($node);} ?> <br /></div>
<?php else: ?>
<div>
<div id= "content">
<div class="row">
        <div class="column grid_12">
                <div class="row">
                        <div class="column grid_6"> <h1><?php print $title ?></h1> </div>
                        <div class="column grid_6"> <?php print $terms ?> </div>
                </div>
        </div>
</div>
.........

The title is set in the page.tpl.php, so it is displayed twice when you output it in your node template.

Thank you.

Youssef

Any thoughts why when I click on on of Taxonomy Terms, example "MP3", The title for this taxonomy page is not showing. Thanks.

Is this still relevant for version 7.x? If not what has changed?

Drupal 7.0

Please discard.

Thanks.

Hi,

I created a node-nodetype.tpl.php for my content type and place it in my theme file. I am able to customize my node template but when I inserted

<?php
if ($teaser):
?>

<?php
print $node_url;
?>
" rel="nofollow">
<?php
print $title
?>

<?php
print $node->field_nodetype_date[0]['view']
?>

<?php
print $node->field_nodetype_remarks[0]['view']
?>

<?php
else:
?>

This does not print the teaser. I went to views to print the node teaser and it print nothing. Can someone help me if I am doing something wrong as I am still in the learning phase.
Prerna

Prerna.

I would like to show the node teaser in the standalone page, instead of the full node, but not sure how to accomplish it. Additionally, I would like to open the full text in a modal box. I know how to accomplish that for a manually added link, but how to achieve that programmatically for all "Read More links" of a content type?

I appreciate any help on this one.

I am having the same problem, using node--directory.tpl.php.
The following is not working:

<?php if ($teaser): ?>
<?php
    $author
= user_load($uid);
    if (
in_array('premium user', $author->roles)): ?>

<div style="width:450px;height:250px;border:2px solid black;">
<?php endif; ?>
<?php endif; ?>

BUT THIS part of code worked before 'if teaser' just fine:
<?php
    $author
= user_load($uid);
    if (
in_array('premium user', $author->roles)): ?>

<div style="width:450px;height:250px;border:2px solid black;">
<?php endif; ?>

Anybody got any ideas?