
PHPTemplate Intro and Resources – Drupal Theme Engine

Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more
PHPTemplate is the theme engine that was used in Drupal from version 4.7 up until Drupal 8.x. 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, in 2012, with Drupal version 8, a new templating engine – Twig -was introduced. Twig replaced PHPTemplate.
The main reason for the change was the fact that many people struggled with using the PHPTemplate system. P
lus, with Drupal 8, developers wanted to shift to Object-Oriented PHP instead of procedural PHP, which was the standard of previous versions.
Developers say Twig makes 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 was called PHPTemplate and it contained phptemplate.engine
which was responsible for parsing the PHP variables in various template files.
The phptemplate.engine
file contained 2 hooks: hook_init()
and hook_theme()
. Together, those two hooks called the necessary files such as CSS and JS as well as registered the theme’s functions and templates that were used in the theme.
The only file necessary for the theme engine to work was the page.tpl.php
file.
Some themes used only the basic version of this file, while others employed 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.
Drupal states that PHPTemplate has “problematic security” and advises: “Don’t use it unless you know what you are doing and are prepared to implement your own text sanitization.”
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.
- Open Discussion on Drupal’s Theming Capabilities and Templating Engines: older resource that includes thoughts from PHPTemplate’s developer on why he created it and the functionality he planned to include.
- Building a New Theme for Drupal 7: an in-depth tutorial that explains how the theme engine works and guides the user through the necessary steps to creating a working theme for Drupal 7.
- PHPTemplate: official Drupal page with release history and commits.
- PHP Template Engines: an overview of different PHP template engines with code comparisons and examples.
- Theming Patterns of Drupal’s PHPTemplate and Twig Engines: a comparison of PHPTemplate and Twig templating engines.
- Templating Engines in PHP: an article by Twig’s author that outlines why PHPTemplate is not a good choice as a templating engine and how Twig came about.
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:
- Google Rankings: Understand, Diagnose, and Fix: what good is a website if no one knows about it? Learn all about getting the Google ranking you deserve.
- The Ultimate List of Webmaster Tools A-Z: find all the tools you need to make managing your site easy.

Drupaler looking for a great deal on hosting?
A2 Hosting scored #1 in our recent speed and performance tests. They support Drupal and provide free site migration. Save up to 50% on their dev-friendly plans by using this discount link.
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.
Comments