PHP Template Engines

       By: Max Kosyakov
Posted: 2011-03-13 01:19:28
The overall goal of this article is to advocate the use of template engines in PHP applications and to provide argumentation to support template engine usage. It also contains several examples of template engines, their advantages, and shortcomings, as well as a short guide for selecting the right template engine for your Web-based projects.What Is a Template EngineTemplate engine is in a sense a component that allows separation of php and HTML codes. When template engine is used in the application, HTML code is placed into separate files, templates. In addition to the HTML code, these files contain processing instructions that template engines execute, assembling the final HTML page which is downloaded and converted by browsers into what we see as Web pages. Typically, template engines execute a number of processing instructions, including, but certainly not limited to inserting a value of a certain variable, looping through a set of listings, displaying their titles, and including other template(s).Key Reason to Use Template EnginesA task of separating the work of programmers from the work of designers is the most typical reason to use template engines in PHP or other languages. Certain authors covering this subject say that with the help of template engines, designers can create the look of a Web page without any substantial involvement of Web programmers. If there is only one person working on a Web application, there is no need for a template engine.I consider this statement a bit misleading. The biggest advantage of any template engine is to separate the roles of a business domain developer (a programmer), and a front-end developer (designer) even if both roles are performed by the same person. These tasks, while being deeply interrelated, still require different skills and capabilities. When programmers work on the business logic of a website, they need to concentrate on the proper application design, architecture, data structures, business rules, and information storage.Having that many tasks in mind, one can hardly pay enough attention to the look and feel of the website. When the development of functionality is complete, developers can start working on the appearance of the application. If the business logic code (php) and presentation code (HTML) are mixed, then it takes substantial effort to develop and maintain the appropriate look and feel of Web pages generated by such code. Such approach is slow and error-prone.With php code separated from HTML code, it is so much easier to maintain both independently, even for a single developer. It allows developers to concentrate on specific tasks, improving the quality of both the implemented functionality and its visual presentation.Therefore, using template engine benefits Web projects both in short and long term, and that is, regardless of a number or developers working on it.A Very Basic Php-Based Template EngineMany opponents of template engine usage (if they still exist) use the following statement as their main argument: PHP itself is a template engine, why use another one on top of PHP? My answer to this is as follows: it is true, there is no need for another one, PHP is well-suited to take care of this issue nicely. However, it all works fine if we keep the business logic and presentation parts of code in different files.
Trackback url: https://article.abc-directory.com/article/8484