Template files are written in PHP and contain a series of includes and conditional statements designed to detect the presence of elements that must be added into the final output. The includes and conditional statements relate to things like the content of the site title, the presence and location of a logo file, the number of active regions, boxes, etc. Whether a statement is satisfied, and the content displayed, is often the product of decisions made by the site administrator in the process of configuring the site as well as decisions made during the creation of content and functionality.
For example, the segment of code below shows the head of a basic page .tpl. php file.
<?php print $head; ?> <?php print $styles; ?> <?php print $scripts; ?>
The highlighted lines, above, show the include statements in action; in this case, calling into the template file a variety of information including: the page title
($head_title), the head information ($head), the style sheets ($styles), and any necessary scripts ($scripts).
The example below shows a typical application of a conditional statement, again from inside the page .tpl. php file:
In this segment, you see a conditional statement testing whether the $site_slogan returns as true (i.e., it exists) and if so, it prints the site slogan ($site_slogan). You will also note that the site slogan is wrapped with a div with an id of s ite- s l ogan. This is our first taste of how CSS integrates with the templates to control the presentation on the screen.
Whether the site slogan is displayed is determined by a parameter specified by the administrator in the Theme Configuration Manager (discussed in Chapter 2).
The slogan text is set by the administrator in the site information manager. This parameter's value is stored in the database of your Drupal site.
The choices made by the administrator are stored in the database as $site slogan with the value: This is the slogan for my site. $site slogan is then displayed courtesy of a conditional statement in the page .tpl. php file.
Putting all this together, it works like this:
1. The page. tpl. php looks in the database for the string named $site_slogan.
2. If there is a value for $site_slogan, page. tpl. php then prints that value on the screen.
3. The user's browser applies to the resulting site slogan, the styling specified by the div with the id "site-slogan".
The div styling in this case is located in the file style. css, which is also included in the specific theme's directory. Note also that style .css is present courtesy of the actions of the PHPTemplate. The style sheets are included via the statement:
which appears in the head of the page .tpl. php file, as was shown in the previous example.
In summary, a complete Drupal theme consists of a number of template files that are combined at run time to present a coherent single web page. The exact number of templates involved and the nature of their contents will vary from theme to theme.
Was this article helpful?