PHPTemplate Introduction and Resources

PHPTemplate is the theme engine used in Drupal since version 4.7. In layman's terms, a theme engine is what is used to run the code in the templates. It's a way of outputting PHP in HTML, without having to use PHP syntax or PHP tags. This ultimately leads to code separation and allows easier styling, even for those who aren't proficient in PHP.

Brief History

PHPTemplate was built by developer Adrian Rossouw specifically for use with Drupal. It was included in Drupal versions 5 and up, however, specifying the theme engine was still required in the .info file. In Drupal version 7, it was no longer needed and PHPTemplate engine was established as the default. However, with Drupal version 8, a new templating engine has been introduced which completely replaces PHPTemplate.

The main reason for the change was the fact that many people struggled with using the PHPTemplate system. Plus, with Drupal 8, developers wanted to shift to Object-Oriented PHP instead of procedural PHP, which was the standard of previous versions. The new templating engine is called Twig and developers hope it will make it easier to work with themes in Drupal.

Twig

Twig marks a new era when it comes to Drupal templating engines. It works by compiling templates down to plain optimized PHP code and has a sandbox mode to evaluate untrusted template code. It was created by Fabian Potencier, the creator of the Symfony framework and is released under the BSD license. Aside from being used in Drupal 8, it also powers projects like Symfony, eZ Publish, phpBB, Piwik, OroCRM, and more.

How PHPTemplate Works

Drupal's core installation includes a variety of folders containing different files necessary for it to work. One of those folders is called PHPTemplate and it contains phptemplate.engine which is responsible for parsing the PHP variables in various template files. The phptemplate.engine file contains 2 hooks: hook_init() and hook_theme(). Together, those two hooks call the necessary files such as CSS and JS as well as register the theme's functions and templates that are used in the theme.

The only file necessary for the theme engine to work is the page.tpl.php file. Some themes use only the basic version of this file, while others employ a wide variety of additional template files that style specific content or screen space.

PHPTemplate Alternatives

Several template engines exist for Drupal and have been used in some themes, although not as much as PHPTemplate which was the most popular one for a long time. The most notable ones include:

  • Smarty: like PHPTemplate, Smarty facilitates the separation of presentation from application logic but uses a much simpler syntax and compiles copies of the templates as PHP scripts. This allows developers to have the benefits of both the template tag system and the speed of PHP.
  • PHPTal: another engine that uses a much simpler syntax and moves the presentation into the (X)HTML attributes rather than using tags.

Current Status of PHPTemplate

Since the introduction of Twig in Drupal 8, PHPTemplate has been minimally maintained although it remains under active development.

Resources

The following list of resources includes tutorials, videos, and books dealing with PHPTemplate.

Online Resources

Here is a collection of articles and tutorials explaining the basics of the PHPTemplate engine and its replacement, Twig.

Video Tutorials

This is a small collection of video tutorials with practical examples geared for visual learners.

  • Preparing for Drupal 8: a series of video tutorials designed to make the transition from Drupal 7 to 8 as easy as possible with an explanation of why PHPTemplate was replaced and how the new engine, Twig, works.
  • PHP for Themers: a series of video tutorials dedicated to using the PHPTemplate engine and creating themes for Drupal 7.

Books

Although no book deals specifically with the PHPTemplate engine, several related books have whole chapters dedicated to it with the aim of helping you master theme and module development.

  • Drupal 5 Themes (2007) by Ric Shreves: this book about creating themes for Drupal 5 has a chapter dedicated to PHPTemplate engine and goes into great detail explaining how it works with practical examples.
  • The Definitive Guide to Drupal 7 (2011) by Melancon, et al: a thorough guide to Drupal 7 with a detailed chapter on PHPTemplate and using it to create themes.
  • Learning Drupal 6 Module Development (2008) by Matt Butcher: this book provides a practical hands-on approach to developing custom Drupal modules with a strong focus on themes using the PHPTemplate engine.

Moving on From PHPTemplate

PHPTemplate allowed Drupal developers to create very powerful themes which could (and often did) completely override the functionality of basic templates. It allowed for a complete separation of logic and design and gave experienced developers a way to create custom functionality for the needs of their themes. By using the resources above, you should become familiar with PHPTemplate and learn how to move forward in development, with or without it.


Further Reading and Resources

We have more guides, tutorials, and infographics related to website development and management:

Ultimate Guide to Web Hosting

Check out our Ultimate Guide to Web Hosting. It will explain everything you need to know in order to make an informed choice.