php latte best php templating engine omniceps

A PHP templating engine is the one which gives you ability to write html for your clients efficiently using PHP variables.

However, PHP was originally built to be a templating engine, but yet it was never used primarily as a templating engine. There has been so many PHP templating engine so far in the market since PHP evolved, but none of them has made as great impression on us as Latte. Introducing you to Latte, one of the best PHP templating engine you have come across so far. Also Latte protects your web app from vulnerabilities like XSS (cross site scripting).

  • So basically Latte is really very fast in terms of processing time.
  • Latte is safe and more protective, since it uses content-aware escaping.
  • Latte is very simple to understand and use as it uses self-descriptive syntax.

What makes Latte Best PHP Templating Engine:

Lets look at why PHP was never popular as a templating engine by the example below. Suppose you want to print a list of all available cars:

Above code is not very easy to understand and also it requires you to call htmlSpecialChars function explicitly. That’s why PHP never succeeded as a templating engine. Now, check below how we can write the same code in Latte which is part of Nette Framework. You are going to just love its simplicity.

As evident from above there are basically 2 types of macros in Latte:

1. Inside braces macros, for example {foreach …}
2. Tag based macros n:macro, for example n:if=”…”

How to Install Latte:

Latte can be installed in 2 ways, best way is download latest release from here – https://github.com/nette/latte/releases, or we can also use composer as below:

Also check the GitHub link – https://github.com/nette/latte. PHP version 5.4.4 or newer is required for Latte and its also supported by PHP 7.1 and 7.0.

Rendering the templates can be done by just running the code below:

 

Macros in Latte:

All built in macros are explained in detail here – https://latte.nette.org/en/macros, along with this you can also create your own macros.

As explained above there are two types of macros supported, n:macro and brace based macros like {foreach}.

Brace macros are explained by below example:

Adding cherry to the top Latte has a {* commenting macro *} which won’t be printed in the template output.

n:macros

We have already seen that n:macros have to be written into the HTML tags like their attributes. Also, every macro pair (like {if} — {/if}) can also be written as an n:macro. That macro will now correspond to its parent HTML element:

Prints:

If we use the inner- prefix then the macro will only be applied to that element’s body:

Prints:

Filters in Latte:

Please visit here to read about filters in detail -> https://latte.nette.org/en/filters

You can use pipe sign notation to call filters in Latte, its called as modifier |.

Chaining of filters is also allowed from left to right

Parameters can be added after the filter name using colon (:) or comma (,)

Latte also supports custom filters and universal filters. Please read in more details on official website.

Performance:

The speed of latte comes from the fact that templates are compiled to their native PHP counterparts and are stored/cached on the disk. So the speed of templates nearly equals pure PHP.

Auto-recompilation of templates is done when the source file is updated. During development you will just need to update the template and it should reflect the changes.

Read about Laravel Horizon. Best queues implementation system for PHP applications. Click Here ->

Context Aware Escaping in Latte:

There is no need of manual work in Latte when it comes to escaping, its smart context-aware technology does escaping automatically for you. Plus its very consistent and accurate as well. So that you will not have to worry about the security loopholes ever.

Check below as an example of how context aware escaping works in Latte:

If $title stores a name like ‘Terminator & 13 1/4’ as string it will generate the below output. Check carefully the difference in escaping done in HTML and JavaScript parts:

Due to the smart context-aware escaping your application will always be safe against attacks like XSS (cross-site-scripting).

Pretty Output in Latte:

All tags are automatically indented properly when generated via Latte, the code looks like it has been beautified using a professional code beautifier.

Latte also supports User defined Macros and template from string as well. Please visit the official site to read more on these. So this was Latte from Nette one of the best php templating engine.

Read about 9 best open source tools for developers today. Click Here ->