HomePHP Developing an Extensible Template Processor in PHP 5
Developing an Extensible Template Processor in PHP 5
This is the first part of a three-part series that covers separating logic from presentation in PHP applications. In this article you will learn to develop a template system that is advanced enough to meet the requirements of a majority of applications.
A downloadable file for this article is available here.
Introduction
In consonance with the evolution of PHP to the mature multi-purpose programming language that you see today, template systems have largely grown in popularity, due to their ability to separate the driving logic of an application from its visual presentation.
Definitely, splitting your PHP applications into different sections or layers, which are conceived in most of cases as the data, the application and the presentational layers respectively, helps to make the entire program more flexible and maintainable. In general terms, when you use a template system to keep the application's logic isolated from the visual appearance, usually you're going to work with one (or many) template files that contain a specific number of placeholders, which will be filled with real data.
As you probably know, replacing placeholders with actual data (coming either from database tables, flat files or other sources) is the responsibility of the application layer, which implements the required "smart" logic that substitutes data markers with real content. Taking into account the basic functionality provided by most PHP template systems, you can pick up a particular software package in accordance with the size and complexity of the application that you're currently developing, and of course in consonance with your own expectations.
Particularly when it comes to choosing a specific template system, you'll find a plethora of options, ranging from basic placeholders replacing libraries, to full-featured systems like the popular Smarty, which eventually might require learning proprietary template statements, in order to get the most out of it.
Nevertheless, it's possible to develop an intermediate template system that meets the requirements of a vast majority of applications, without the need to appeal to basic packages or having to deal with the numerous features of Smarty. It's precisely for this reason that this series will be focused on developing an extensible template processor in PHP 5. The template system that I plan to build will expose a handy set of features, which hopefully will satisfy the most common requirements of modern template software, without the need to learn a new "template-oriented" sub language.
Are you ready to learn how to code an expandable PHP 5 template processor? Right, let's get started.