Look at the Theme Engine Files

Inside the PHPTemplate directory on the server (themes/engines/phptemplate), you will find the following:


T 3 themes

► chameleon T L engines

T [ -= ptipternplate at block,tpl.php & box.tpl.php j comment,tpl.php # default.tpl.php j node.tpl.php _ phptempfate.engine

► [..:■ garland pi pushbutton d update, php ¿1 UPCRADE.txt a xmlrpc.php

Default templates -Template engine

The default template files contained within the PHPTemplate directory provide the most basic level of formatting, necessary for the styling of various page elements. Here's a brief overview of each of the files contained inside the theme engine directory, along with a short summary of their key functionality:


<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="block block-<?php print $block->module ?>">

<h2><?php print $block->subject ?></h2> <?php endif;? >

<div class="content"><?php print $block->content ?></div> </div>

This template file is used to style the block presentation on the site. Note that the key elements here are the placement of the block subject (note, this is the block's title) and the block's content. The other statements in this file are simply formatting.


<div class="box"> <?php if ($title): ?>

<h2><?php print $title ?></h2> <?php endif; ?>

<div class="content"><?php print $content ?></div> </div>

This file sets up the wrapping of the content with a "box"—that is, a div tag that allows you to format the content along with a title for the box. Note the key elements here are the display of the box's title and the content, each wrapped by styles.


<div class="comment<?php print ($comment->new) ? ' comment-new' : ''; print ($comment->status == COMMENT_NOT_PUBLISHED) ? ' comment-unpublished' : ''; ?> clear-block">

<?php print $picture ?> <?php if ($comment->new) : ?> <a id="new"></a>

<span class="new"><?php print $new ?></span> <?php endif; ?>

<h3 ><?php print $title ?></h3> <div class="submitted">

<div class="content">

This file sets up the display of user-submitted comments to posts and to the forum.

Note that the multiple print statements here control the display of all aspects of the comment content, including the user's picture, if this option is selected.


<!-- PHPTemplate was instructed to override the <?php print $hook ?> theme function, but no valid template file was found. -->

This file is a fallback—a safety net. In situations where a function lacks a valid template, this file is called.


<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?> clear-block">

<?php print $picture ?> <?php if ($page == 0): ?>

<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2> <?php endif; ?>

<div class="meta"> <?php if ($submitted): ?>

<span class="submitted"><?php print $submitted ?></span> <?php endif; ?> <?php if ($terms): ?>

<span class="terms"><?php print $terms ?></span> <?php endif;?> </div>

<div class="content">

Any time a node is rendered, this file is used. This file is the most complicated of the theme files in this directory, and that is because it does a lot of the heavy lifting on the site; this one file works with all the nodes in their many forms.

block, box, comment, and node (discussed above) are only the basic default functions. There are, however, many additional functions that can be styled using PHPTemplate. A list of themeable functions and their application is included in the Chapter 4.


It's an understatement to say that a lot goes on in this file; a review of the source code of this file will go a long way towards helping you gain an understanding of the big picture of how PHPTemplate assembles the output. Unfortunately, a complete dissertation on the inner workings of PHPTemplate is beyond the scope of this book. Accordingly, I have only highlighted two sections that are of particular interest to anyone who wants to understand how to work with themes.

The first highlighted section enables the regions for use in the theme. /**

* Declare the available regions implemented by this engine.

* An array of regions. The first array element will be used as the default region for themes.

function phptemplateregions() { return array(

'left' => tt'left sidebar'), 'right' => t('right sidebar'), 'content' => t('content1), 'header' => t('header'), 'footer' => t('footer')

Note that the above section is perhaps the only place where I will ever endorse directly modifying any file contained in the theme engine directory. You may wish to modify this file if you wish to add or re-name a region across multiple themes; in any other circumstance, I strongly recommend that you stay completely away from making changes to these files. If you need to override these files, do so by creating alternative versions of them that are placed inside the theme directory, alongside the page .tpl. php file. This topic is discussed at length in later chapters dealing with intercepts and overrides.

Note the ' t' function in the above excerpt. This function is related to the translation function, which allows Drupal to show the name for the region in the chosen language inside the administration interface.

The second highlighted section is informational. In this excerpt, the order of precedence among template files is defined. The comments in the code here are very useful; note the example showing how the system will respond to a theme file along each element of the path:

// Build a list of suggested template files in order of specificity. One

// suggestion is made for every element of the current path, though

// numeric elements are not carried to subsequent suggestions. For example,

// http://www.example.com/node/lZedit would result in the following

// suggestions:

// page-node-edit.tpl.php // page-node-l.tpl.php // page-node.tpl.php // page.tpl.php $i = 0;

$suggestion = 'page'; $suggestions = array($suggestion); while ($arg = arg($i++)) {

$suggestions[] = $suggestion . '-' . $arg; if (!is_numeric($arg)) {

if (drupal_is_front_page()) { $suggestions[] = 'page-front';

return _phptemplate_callback('page', $variables, $suggestions);

The mechanism provided in the example sets out an important principle that is, the order of precedence in the event of the presence of multiple template files. This hierarchy makes it possible for a developer, like you, to create specific templates for specific elements. The option to create themes that can be associated with every element on the path creates a great deal of PHPTemplate's flexibility. Learning to take advantage of that flexibility is one of the key goals of this book.

A Look at the Key PHPTemplate File Contained in the Theme

The template files contained inside the themes/engines/phptemplate directory are all linked to another file, page .tpl. php, which is located inside the individual theme directory. This file is key to enabling PHPTemplate within a theme.

Some themes use only the basic page .tpl. php file to achieve the look and functions the developer desires, others contain a wide variety of additional template files that serve to style specific content or screen space.

For this example, I am using the page .tpl. php file from the theme Zen. Zen is not only a representative example of a typical page .tpl. php file, but also a particularly useful example due to good use of comments within the code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www. w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/19 9 9/xhtml" lang="<?php print $language ?>" xml:lang="<?php print $language ?>">

<title><?php print $head_title; ?></title> <?php print $head; ?> <?php print $styles; ?> <?php print $scripts; ?> </head>

<?php /* different ids allow for separate theming of the home page */ ?>

<body class="<?php print $body_classes; ?>"> <div id="page"> <div id="header">

<div id="logo-title">

<?php print $search_box; ?> <?php if ($logo): ?>

?>" title="<?php print t('Home'); ?>"> <img src="<?php print $logo;

?>" alt="<?php print t('Home'); ?>" id="logo" />

</a> <?php endif; ?> <div id="name-and-slogan">

<?php if ($site_name): ?> <h1 id='site-name'>

<a href="<?php print $base_path ?>"

title="<?php print t('Home'); ?>"> <?php print $site_name; ?> </a>

</hl> <?php endif; ?> <?php if ($site_slogan): ?> <div id='site-slogan'>

<?php print $site_slogan; ?> </div> <?php endif; ?>

</div> <!-- /name-and-slogan --> </div> <!-- /logo-title -->

<div id="navigation" class="menu <?php if ($primary_links) { print "withprimary"; } if ($secondary_links) { print " withsecondary"; } ?> "> <?php if ($primary_links): ?>

<div id="primary" class="clear-block">

<?php print theme('menu_links', $primary_links); ?> </div> <?php endif; ?>

<div id="secondary" class="clear-block">

<?php print theme('menu_links', $secondary_links); ?> </div> <?php endif; ?> </div> <!-- /navigation --> <?php if ($header || $breadcrumb): ?> <div id="header-region">

<?php print $breadcrumb; ?> <?php print $header; ?> </div> <?php endif; ?>

<div id="container" class="clear-block"> <?php if ($sidebar_left): ?>

<div id="sidebar-left" class="column sidebar">

<?php print $sidebar_left; ?> </div> <!-- /sidebar-left --> <?php endif; ?>

<div id="main" class="column"><div id="squeeze">

<?php if ($mission): ?><div id="mission"><?php print $mission;

?></div><?php endif; ?> <?php if ($content_top):?><div id="content-top"><?php print $content_top; ?></div><?php endif; ?>

<?php if ($title): ?><hl class="title"><?php print $title;

?></hl><?php endif; ?> <?php if ($tabs): ?><div class="tabs"><?php print $tabs;

<?php print $help; ?> <?php print $messages; ?> <?php print $content; ?> <?php print $feed_icons; ?>

<?php if ($content_bottom): ?><div id="content-bottom"><

?php print $content_bottom; ?></div><?php endif; ?> </div></div> <!-- /squeeze /main --> <?php if ($sidebar_right): ?>

<div id="sidebar-right" class="column sidebar">

<?php print $sidebar_right; ?> </div> <!-- /sidebar-right --> <?php endif; ?> </div> <!-- /container --> <div id="footer-wrapper"> <div id="footer">

<?php print $footer_message; ?> </div> <!-- /footer --> </div> <!-- /footer-wrapper --> <?php print $closure; ?> </div> <!-- /page --> </body> </html>

[rltX You can download your own copy of the Zen theme from I

http://drupal.org/project/zen. I

Let's break down this template file, and look at it in bite-sized functional units (we'll leave the CSS until next chapter):

The following code creates the head of the resulting page. The PHP statements in this excerpt include in the resulting web page: the page title, the various bits of head data including the metadata, the style sheets, and the scripts:

<title><?php print $head_title; ?></title> <?php print $head; ?> <?php print $styles; ?> <?php print $scripts; ?> </head>

This next excerpt begins just inside the beginning of the body of the page The PHP statements here are all conditional—they will only produce output visible to the viewer when the conditions are true. This section includes the optional items controlled by the site administrator, such as the search box, the logo, the site name, and the site slogan. If the administrator has not enabled any of these items, they will not be displayed on the page:

<div id="iogo-titie">

<?php print $search_box; ?> <?php if ($iogo): ?>

?>" titie="<?php print t('Home'); ?>"> <img src="<?php print $iogo;

?>" ait="<?php print t('Home'); ?>" id="iogo" />

<div id="name-and-siogan">

<?php if ($site_name): ?> <hl id='site-name'>

<a href="<?php print $base_path ?>"

titie="<?php print t('Home'); ?>"> <?php print $site_name; ?> </a> </hl> <?php endif; ?>

<?php if ($site_siogan): ?> <div id='site-siogan'>

<?php print $site_siogan; ?> </div> <?php endif; ?>

</div> <!-- /iogo-titie --> This excerpt shows this theme's handling of the navigation:

<div id="navigation" ciass="menu <?php if ($primary_iinks) { print "withprimary"; } if ($secondary_iinks) { print " withsecondary"; } ?>

The following lines relate to the display of the primary links:

<div id="primary" class="clear-block">

<?php print theme('menu_links', $primary_links); ?> </div> <?php endif; ?>

The next segment deals with the secondary links:

<div id="secondary" class="clear-block">

<?php print theme('menu_links', $secondary_links); ?> </div> <?php endif; ?> </div> <!-- /navigation -->

This excerpt shows the display of the breadcrumb trail. It also shows the first of this theme's regions, in this case, the header region. In this theme, the header region is declared and active, enabling the site administrator to assign blocks to the region:

<?php if ($header || $breadcrumb): ?> <div id="header-region">

<?php print $breadcrumb; ?> <?php print $header; ?> </div> <?php endif; ?>

Note that activating a region has two pre-requisites: it must be placed in the page .tpl. php file, and the region must also be declared in the template . engine file. Adding additional regions to a theme is discussed in detail in later chapters.

This short statement places the left sidebar region on the page. As this theme uses a conditional statement to place this left-hand column on the page, the column will neatly collapse and disappear from view if nothing is assigned to the space:

<div id="sidebar-left" class="column sidebar">

<?php print $sidebar_left; ?> </div> <!-- /sidebar-left --> <?php endif; ?>

This busy excerpt shows a number of events, all of which are associated with the presentation of content items. The statements relate the display of information and functionality with the main content area of the theme:

<div id="main" class="column"><div id="squeeze">

First is a conditional statement that will display the mission statement (if there is one and it has been enabled by the site administrator):

<?php if ($mission): ?><div id="mission"><

?php print $mission; ?></div><?php endif; ?>

The next line places the content top region on the page:

<?php if ($content_top):?><div id="content-top"><?php print $content_ top; ?></div><?php endif; ?>

Next, comes the Item's title:

<?php if ($title): ?><hl class="title"><?php print $title;

then, the Tabs:

<?php if ($tabs): ?><div class="tabs"><?php print $tabs;

Next, comes the Help link:

The next line places the content region on the page:

This places the feed icons:

The next segment inserts the content bottom region on the page. This region, and the content top region appear often in themes. The content top and content bottom regions are typically used by the Drupal system in the layout of certain content items; these regions are not generally available for assignment of blocks:

<?php if ($content_bottom): ?><div id="content-bottom"><

?php print $content_bottom; ?></div><?php endif; ?> </div></div> <!-- /squeeze /main -->

In later chapters, we will look at how to enable these regions and make them eligible for block assignment.

This excerpt places the right sidebar region on the page. As this theme uses a conditional statement to place this right-hand column on the page, the column will neatly collapse and disappear from view if nothing is assigned to the space:

<div id="sidebar-right" class="column sidebar">

<?php print $sidebar_right; ?> </div> <!-- /sidebar-right --> <?php endif; ?>

This excerpt places the footer region on the page, and also the footer message, if the administrator has included one:

<div id="footer-wrapper"> <div id="footer">

<?php print $footer_message; ?> </div> <!-- /footer --> </div> <!-- /footer-wrapper -->

Was this article helpful?

0 0

Post a comment