Why You Should be Using Templating Engine Instead of PHP

By: Sunil Kumar |  In: Web Developement  |  Last Updated: 2017/12/24

Why You Should be Using Templating Engine Instead of PHP

I love PHP but when I see a lot of PHP tags inside an HTML file, Its kind of painful for my eyes. It’s fine for simple one with fewer PHP tags but when we use a lot of nested loops with if…else statements it becomes unreadable.
I know Some people are comfortable with using  PHP embeded with HTML But there a lot of people who completely hate it. and believe me, you will also hate it once you start using a templating engine.
There are many reasons to use a template engine rather than printing your code in a given language. Some of them are I am trying to list here.


Complex applications need to separate all the layers starting from business logic to presentation. That was the main reason to create MVC architecture which was later adopted in Web applications also.
There should be always a separation between business logic and the presentation. Because developers and designer have to works on the same project without disturbing each others code and templating engine perfectly allow this.
If you have a good separation of your markup from your logic it becomes easier to debug your code, to share your code with collaborators, and allows for more rapid development by streamlining common methods for outputting your markup.
Separation allows us to use best tools for that particular part. For example, when you are working on the presentation you are more concerned about the look and feel of the page at the same time when you are working on business logic you are concerned about the visuals – you need to focus on data to be represented.
So for PHP have more features as a scripting language instead of the templating language. If you need a templating twig or smarty have way more features as a templating language instead of PHP.
Sometimes we also need to change the backend language like we want to switch the project backend form PHP to java it is easy to reuse the code when there is a separation.


Mixing Business logic with the presentation is not a good idea. If you don’t believe me check any WordPress theme and you will realise how correct I am. I have developed some of the WordPress themes and its a hectic task to manage them as HTML and PHP code are mixed. There is no separation between these two.
It’s not easy to read a code mixed with PHP and HTML tags. Especially when you are editing a code written by someone else.

Template Caching

Templating engines provide a lot of special functions and features but this also brings the complexity of execution. Most of the good templating engine offer caching system for the static files.
Templating engine compiles the file every time, But using caching we can skip this compilation complexity. Template engines, like Smarty and Twig, have better performance with compiled files, which is another caching layer for the web application.
So with the template caching you can save time in rendering your template to the browser which will boost your website page speed.

Template inheritance

The most powerful part of the template engine is template inheritance. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.
So you have to write one single code for one functionality and you can inherit this code anywhere in your project.
For example- header and footer are the most common part of every website which we have to include on almost every page of website But we don’t want to write code for header and footer on every page. It’s not a good practice to write duplicate code for the same functionality. Using template inheritance you can avoid this. We write a block in base file and put this common code inside this block and can inherit this block anywhere in the template.

Extended Functionality

PHP is not designed for templating. It is more of scripting language. PHP has less functionality as we compare to any templating engine in terms of templating.
Templating engines are specially designed to make front-end development easy and convenient. They have some great functionality like inheritance, auto-escaping which make them extended to simple PHP.

Output Auto-Escaping

When generating HTML from a template, there is always a risk that a template variable may output unintended HTML or dangerous client-side code. The result is that dynamic content could break the HTML of the resulting page or allow a malicious user to perform a Cross Site Scripting (XSS) attack.
Consider this classic example:
Hello <?php echo $name ?>
Imagine the user enters the following code for their name:
Without any output escaping, the resulting template will cause a JavaScript alert box to pop up:
Hello <script>alert(‘hello!’)</script>
And while this seems harmless, if a user can get this far, that same user should also be able to write JavaScript that performs malicious actions inside the secure area of an unknowing, legitimate user.
The answer to the problem is output escaping. With output escaping on, the same template will render harmlessly, and literally print the script tag to the screen:
Hello &lt;script&gt;alert(&#39;hello!&#39;)&lt;/script&gt;
This is tremendously good for security. It’s the “secure by default” approach. Output-escaping is by default “ON”  in most of the templating engine.


So there are a lot of reason you should be using a templating engine instead of PHP. They make your code more readable and manageable. It becomes easy to develop for both developers and designers when there is a clear separation between front-end and back-end. It becomes more easy to debug the code also.
There are a lot of templating engine available in the market. I also listed some of the most popular templating engines which can be the best suit for your application.


Leave a Comment

Your email address will not be published.


Sunil Kumar

I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
Know More

Join more than 10,000 others Web Developers