Complex applications need to separate all layers, starting from the business logic, and ending with the presentation. That was the main reason to create the MVC design pattern, which was later adopted for web applications. The projects created in recent years are advanced on many levels and they demand strong engagement from developers and designers. Both have to collaborate, so finding a common language for their own competencies is very important.
A PHP template engine allows developers and designers to work on the same areas, but without disturbing each other. The data fetched and proceeded by backend will be delivered to a template where they can be modified and displayed as final output.
I have worked as both back-end (PHP) and front-end (HTML, CSS) developer, so I have experienced both best practices and expectations. What I have learned is that collaboration is successful only when people understand each other. And template engine was created to help all involved in development, so let’s analyze its advantages.
1. Separation
When an app code grows and begins to be unreadable, developers think about the project files structure. Separation between Model, View, and Controller is required by the design pattern, however developers and designer still need to work on their common code. And the template file is the best place for such collaboration.
2. Readability
Logic and presentation mixed together in project files were not clear enough. Just check any template used by WordPress if you do not believe me. It looks terrible and it’s very hard to understand for the designer.
Developers split the code into separate files because both layers use different technologies, e.g. PHP and HTML. Most often the back-end and the front-end code need to be maintained by different specialists. Is the PHP developer interested in the CSS code used for styling the webpage? Well, I don’t think so. And from the designer’s perspective the same applies to writing the SQL query on the back-end side. That’s the reason why templates are better for all.
3. Caching
Good template engines offer caching for static files. Special syntax brings many features, but it needs to be parsed and compiled with source language. Template engines, like Smarty and Twig, have better performance with compiled files, which is another caching layer for web application.
4. Collaboration
Developers and designers have to work together. Complex projects need many people and experienced specialists. Developers and designers need their own areas in the project to work better. Using popular tools helps them all to understand each other and collaborate seamlessly.
5. Filters, modifiers, and many others
Template engines have built-in features which allow handling popular scenarios easily. If you want to truncate text, format a string or date, set default values and many others, it is all trivial.
The solution offers modifiers and filters for simple tasks, but also allows creating your own plugins and extensions. All these examples are just another layer of abstraction which helps the project.
6. Easy to learn
PHP developers say very often that “PHP is a template language, so there is no point to learn another one”. Considering all advantages, it’s definitely good to learn some template engine language, especially when the language is much easier for these developers, who had previously learned PHP.
Designers can learn simple and intuitive templates syntax much easier comparing to a whole complex language. Printing variables, conditional instructions or loops are very natural and easy to use, even for a non-technical person.
7. Smaller chance of breaking more
The more people work with the same code, the bigger chance of something going wrong. To avoid conflicts the developers created code versioning. The next step limiting possibility of a fatal error comes with access to the restricted area. The designers don’t have to read and edit back-end code. The place for their work is limited to the template files.
Summary
All listed arguments are my personal opinion, but they are the result of many years of experience. You can disagree with them, of course. But on the other hand, it’s hard to agree with using PHP as a template engine in every situation.
Sometimes, when a single developer takes care of the whole project, it makes sense. Otherwise, it’s better for a designer and a developer to separate the code using the templates. The features and extensions available support their collaboration. The final benefits are higher than the costs of learning template engine syntax.