The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

Through the 2010s templating languages have proliferated. Handlebars, Mustache, Twig, Blade and Pug are some of the more popular examples but there are many more as evidenced by this list https://en.wikipedia.org/wiki/Comparison_of_web_template_engines

The vast majority of these use the double bracket {{ notation and many are syntactically almost identical. The fact that there are so many different languages probably has something to do with the vast number of competing frameworks. With many of the frameworks based on collaborative open source projects it only takes a disagreement and a fork of the project for the mitosis to occur and the the technologies to mushroom. This can be seen in the case of Symfony and Laravel.

Grappling with mushrooming languages and approaches is a part of the modern world of technology. Thankfully in the case of brackets based templating languages the constructs are relatively simple and the differences miniscule.

The emergence of the modern(remember PHP itself was originally a templating language) templating language is a response to two things:

Brackets based templating languages think of themselves as logic-less simple constructs. In reality they nearly all possess most of the key attributes of the a fully fledged programming language. The syntax they use is much more straightforward and this is certainly to be welcomed

<p>hello {{user.name}} </p>

is a vast improvement on 

<p>hello <?php echo $user->getName(); ?></p>

The benefits of templating languages could be summarised as follows:

Let's have a deeper look at these

The syntax is simpler

This is really the big advantage. It seems straightforward but the requirement for everything to be simpler means that a lot of program logic must move out of the view. This allows the development of the front end and UX of the system to be done by designers who, very often, are not programmers. Long ago user interfaces were designed by programmers who weren't that interested in the user experience. Thankfully we have moved on from that. The skills and abilities to interpret, design and implement a great user experience, while overlapping, are not necessarily those of a programmer. Double brace programming languages allow for teams where programmers can concentrate on the programming and designers can concentrate on the User eXperience.

Template inheritance

Template inheritance is described in detail in the second post in this section. It can be done without a templating language as demonstrated in that post but it's more straightforward and elegant from within the templating language. Templating languages support the concept of a block which can be placed relative to other blocks when inheriting templates thereby allowing for great control over the discrete elements of a website which can be reused on different pages.

Auto-escaping HTML

When a user is submitting data to a web based system if they need to use characters which are special in html such as < or a single or double quote ' or " then this can cause problems in how the data is subsequently presented on a web page. The angle bracket < will be interpreted by the browser as the beginning of a HTML tag. For this reason angle bracket < must be written in HTML as &lt; > is written as &gt;  and ' is written as &quot; This is called "escaping" the special characters. Rather than having to remember all these codes or look them up a developer using a templating language can turn auto-escape on and use the characters as normal. The templating language will automatically convert them to their escape equivalent code.

They allow for variable filtering

Filters in templating languages allow developers to quickly and easily present data from a variable in a particular format. For example given a variable in Volt called user with the attribute name containing Joe Bloggs then

<p>Hello {{user.name|upper}}<p>

will print JOE BLOGGS to the screen.

A range of other useful filters which are included as part of the volt language they can be found here https://docs.phalconphp.com/en/3.4/volt#filters

Should you use Volt?

There is a case to be made against using templating languages. They introduce another layer of complexity which has to be managed and of course developers and designers will have to learn the language. Whether you decide to use them or not for your project will probably depend on a number of factors such as

As with many aspects of systems development, templating languages are a tool. Deciding what is the appropriate tool and when to use it will be at the discretion of the team or person designing the system. So long as there is a basis for the informed decisions being taken and the arguments on both sides of the debate are well understood then then either approach can work well. On the whole I do feel Volt makes for much more readable code. And if it's easier to read its a lot easier to maintain. You do need to learn Volt but at a basic level its very simple and can be learned in minutes. If you find yourself writing very complex code in Volt you've missed the point and should consider trying to make your views simpler - some of the logic you're creating might really belong in the Controller or the Model.