By 

What is a Template Engine. Is PHP a Template Engine. Why Use Smarty? Stress Testing


Hi guys,  hope its going all good! We are here to talk about some interesting topics, especially for beginners, and some stats and testing results that could be helpful for experts. This post includes the following:

Topics Discussed

·         Template Engines explained with daily life examples

·         Clearing misconceptions about using PHP as a Template system

·         List of different Template systems

·         Smarty Template Engine Benefits, comparison and test results

What are Template Engines?

Template engines allow you to separate design and code, which means you separate application logic from its presentation.  Why would you do so?

Because:

·         It produces smart code, means optimized code that will run better. (We’ll see some examples lator)

·         It takes less time to code

·         It is easy to read, write and edit

·         It is easy to maintain

·         Highly scalable and portable

·         Easy integration, plug n play code, choose where you would like your code to work.

·         And …..

Lets make it more easy with the following practical example:

PHP Code without Template Engines

You can see that HTML and PHP code is embedded  with each other in the same page, making it hard to read, maintain, and upgrade. This is clearly no way near the best practices of coding.  Many programmers still use this practice, but I would suggest not to use it.

This poses another bad situation where you and your design team both work on a single file. Concurrent changes may conflict with each others work, causing frustration, long time to troubleshoot etc. Lets Now introduce a template engine in the above code and see how it makes life easy:

PHP Code with Template Engine

Example.php

Example.tpl

In this example, we have created 2 files, one is a PHP file, and other one is a Smarty file. All the programming stuff goes into a PHP file, where as the display/design part goes into an HTML/Smarty file. This is what a template engine does. It makes the code highly scalable, easy to read and maintain, upgrade etc.

So by using a template system, you separate design and code for performance optimization, scalability and maintenance.Lets now take a more practical approach by write a small piece of code.

Below code will display title and a list of products.

Source

In the above code, we are using a mix of PHP and HTML in one file. HTML tags like <P>, <H2> change the appearnce of information on the frontend. If we want to use a template engine in above code, we will need to separate HTML and PHP code into separate files. For example, here is a PHP file that contains PHP stuff only.

By introducing a template engine, you will remove the $title variable and the $products variable, put this in a separate class, and then call a template file to display the HTML.

In the above code, renderHTML() method passes the variables that you want to display to ‘all-products.html’ file, which contains necessary HTML for output.

This command tells the compiler to use ‘all-products.html’ as a template file, which is given below. For example in future, if design layout changes, or you adopt different theme, you will never need to change this PHP file except the file name at the end, you will just create a new HTML file using these variable. It means designers work separately and developer focus on their code seperately. There is no mix up.

In order to use PHP variables in HTML file, you call these variables by using a special syntax e.g.,( {var}), which is defined by a template system that you use.

Types of Template Engines – Which One to Choose?

There are many template systems for PHP, and the choice is yours, we will be testing out the top two, but lets list down some of the top performing systems.

·         Smarty

·         Twig

·         Blade

·         Mustache

·         Volt

And many more.  I asked about 70 coders, which one would be the closest competitors, and the answer was mostly Smarty vs Twig. But before we talk about these two, make a comparison and show some stats, let us clear some misconceptions about using PHP as a template engine.

Is PHP a Template Engine – Can we use it for this Purpose?

Well, you would say PHP started its life as a template engine but what happened next. Does it offer same set of features that modern template systems are offering? The answer is no. There are no updates. You don’t even have a syntax defined for this purpose. Only a few types of of PHP code are allowed in an HTML file.

In order to use PHP as a template engine, you add PHP code in an HTML file (the downside of using this scheme is mentioned above). But the scary part is that, you are only allowed to use following short tags in this case:

·         Single functions

·         Alternate format If / Else / ElseIf blocks

·         Alternate format For loops

·         Alternate format Foreach loops

·         Alternate format Switch statements

To change the syntax, you will replace the opening brace bracket with colon ‘:’, and closing brace with endif; endfor; endforeach;, or endforswitch;, respectively.

This scheme makes the code a lot slower, because your interpreter would need to jump in and out of PHP tags. But when you start to use more iterations and conditions, things get messy. Here are some examples:

Foreach Example

If / ElseIf / Else Examples

Also, PHP does not take care of encoding output for you whereas a proper template engine defaults to HTML encoding preventing accidental errors and security pitfalls.

So we can say that, PHP is not a proper template engine, although you can try to use it for this purpose with extremely limited scope and high chances of errors.

What is Smarty? Smarty VS TWIG Comparison and Testing

Now that we have talked a lot about what template engines are, its time to tell you that Smarty is one of the fastest PHP template engine. Ecommerce platforms like PrestaShop, uses it  in their framework, which serves as a great testimonial.  We will talk about how to install and use Smarty in our step by step and video tutorials. Now its time to prove why Smarty is the best and fastest.

The closest competitor for Smarty is Twig, which is gaining a lot of popularity as being highly updated and modern template engine.

Smarty Vs Twig – Performance Testing

In order to peform testing, complex scripts were written to obtain noticeable results.

Syntax Comparison:

In our first test, its clear that its easy to write code in smarty. A code in Twig is a bit more complicated as below:

Smarty Syntax

TWIG Syntax

Gettng the Values of Variables

Since this is the most used operation, we are  trying to test the best structure used to store template variables. Several consecutive tests were performed to produce the value of 10000 variables, and here are the results. First, checkout the script in both Smarty and Twig:

Smarty:

{$var0} {$var1} {$var2} {$var3} {$var4} …

TWIG:

{{ var0 }} {{ var1 }} {{ var2 }} {{ var3 }} {{ var4 }} …

Results:

 CompilingExecution
Smarty 3.1.1 16.320 seconds 0.058 seconds
Twig 1.2.0 9.757 seconds 0.083 seconds

Ten thousand syntax structures were complied by both template engines. In compilation, Twig seems to take lead, but understand that complication is only performed once, in the end execution time matter, and we see that Smarty is the winner.

Bypass the Array and Produce values of the Fields

In this test, we are passing an array and accessing the values of 1000 array elements. Here is the script for both:

Smarty:

TWIG:

Results:

 Compiling Execution
Smarty 3.1.1 0.065 seconds 0.009 seconds
Twig 1.2.0 0.131 seconds 0.082 seconds

Above test results shows that Smarty is the ultimate winner in both compilation and execution.

Inheritance

In order to test inheritance, a parent template with 500 blocks and 500 small templates was created that will inherit each other. Here are the results:

 CompilingExecution
Smarty 3.1.1 1.329 seconds 0.002 seconds
Twig 1.2.0 2.641 seconds 0.121 seconds

Smarty combined all the inherited templates into one file  whereas Twig created separate class and file for each template. It shows that Smarty is fast here as well.

Conclusion

We can conclude that Smarty is faster than Twig, it performs better and it optimized the files better.

Information: For this test, Pentioum Dual Core 2 Ghz, with 3 GB of RAM was used by Umumble.  You can download the code and test on any computer.

Well folks, so not you know what are template engines, how do they work, which one is faster. Let me know your thoughts, if you have any news to share, drop some nice comments.

Sohail ahmed
About me

<p>Love to train and learn. SEO and development is my profession. 10+ years of IT training, practical experience in SEO. Creativity helps me strategize things well, especially when i code.</p>

Leave a comment

Your email address will not be published. Required fields are marked *