concept template engine in category php

This is an excerpt from Manning's book PHP in Action: Objects, Design, Agility.
A moderately simple web application might have relatively little use for advanced domain objects. But the rest of the application can still benefit from using objects. Both database access and user interaction can gain from using object-oriented techniques. These objects have even less of a resemblance to “real” objects. Some of them might represent fragments of what the user sees in the application, but often the objects are “engines” that process, organize, or move data around. Template engines (described in detail in chapter 13) are an interesting example. In the section on the Adapter design pattern in chapter 7, we will see the difference between the objects used by two popular PHP template engines: Smarty and PHPTAL. PHPTAL objects represent something almost “real” (or at least familiar to anyone with experience of PHP web programming), a template containing HTML markup. A Smarty object, on the other hand, is an engine that can process any template. You feed the Smarty object a template and it generates the HTML output. Other types of objects that are commonly used in web programming are controllers and filters to process and interpret user input and objects that transform and move data into and out of a database.
Depending on your point of view, this might seem either harebrained, too obvious, or both. On the one hand, template engines are tailor-made for template handling, whereas PHP is a full programming language. On the other hand, what the template engines do is suspiciously similar to what PHP does in the simplest cases: they take a file that’s mostly HTML and replace some special markup with dynamic content.
The primary objective of template engines is to facilitate separation of presentation from application logic and content. You can achieve that separation by simply keeping them separate, but it requires discipline. A first step in that direction could be to keep nearly all the PHP code in the beginning in a single PHP section followed by an HTML section, instead of interspersing PHP processing sections in the HTML code. But we can make the separation even clearer by keeping them in separate files. Listing 13.2 shows what the PHP script file looks like after the HTML code has been moved into a separate file called userlist_template.
Listing 13.2. The user list PHP code after extracting the HTML section
<?php require_once 'UserFinder.php'; require_once 'HTTPPlus.php'; $finder = new UserFinder; $users = $finder->findAll(); include 'userlist_template.php'; ?>This is clearly not rocket science, but it’s worth considering. Template engines have some advantages beyond just basic separation of presentation and program logic. On the other hand, none of them are likely to be faster than plain PHP.
In PHP, the standard answer to the perennial question of how to separate presentation from domain logic is to use a template engine. The template engine concept will be familiar to PHP programmers, but may be confusing to those who are used to different terminology that is prevalent in some other languages, particularly C++ and Java. (C++ templates and the Template Method design pattern [Gang of Four] both refer to template concepts that are distinct from the one discussed here.)
A template in our context is a web template—a page written in a language that consists mostly of HTML markup, but also has a way of expressing how dynamically generated content is to be included. A template engine is a software library that allows us to generate HTML code from the templates and specify the dynamic content to be included.
This is the basic idea behind PHP itself, JSP, and ASP. That means there is something mildly paradoxical about the idea of a template engine for PHP, since all template engines do the thing that PHP is perfectly capable of on its own: they generate HTML pages with embedded dynamic content.