18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Drupal</strong>’s Theme Layer<br />

Theme engines<br />

Some themes require alternate theme engines. Theme engines can provide alternate<br />

template syntax, naming standards, and helper functions. Several theme engines are<br />

available for download at http://drupal.org/project/theme+engines. However,<br />

we won't be discussing any theme engines except for <strong>Drupal</strong>'s default theme engine,<br />

PHPTemplate. The PHPTemplate theme engine has been the default theme since<br />

<strong>Drupal</strong> 4.7, has been continuously improved with each version, and has proven its<br />

worth again and again. Over 99% of themes available for download on drupal.org<br />

use the default PHPTemplate theme engine. All of the examples in this book assume<br />

you are using PHPTemplate. So, enough said.<br />

Two ways to theme<br />

So now that we have a good understanding of higher level concepts, let's get down<br />

to the nitty-gritty of theme implementations. As mentioned earlier in the chapter,<br />

there are actually two different ways to implement a theme hook:<br />

• Theme functions: pass data to a PHP function to wrap it in markup<br />

• Templates: pass data to a template which is a PHP file mixed with markup<br />

and PHP print statements<br />

Let's look at each of these in turn.<br />

Theme functions<br />

For a module developer, the easiest type of implementation to understand is a theme<br />

function. Theme functions just need to follow a few simple rules in order for them<br />

to work properly.<br />

First, the name of the theme function follows the pattern:<br />

theme_[theme hook name]<br />

Since the theme hook name is used directly in the theme function's<br />

name, theme hook names have the same constraints on naming as<br />

regular PHP function names; the only valid characters in theme hook<br />

names are alphanumeric characters and underscores. So if a module<br />

has created an example_format theme hook, it would implement it<br />

with theme function named theme_example_format().<br />

[ 66 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!