📜 ⬆️ ⬇️

Histone is a new open source cross-platform template engine.

Introduction

MegaFon is a dynamic technology company whose work is not limited solely to the provision of communication services. For example, among our assets there are a large number of Internet sites where customers receive various information and services. As part of a project to improve and develop our web projects, we, among other things, create products that can be useful to other developers. Today we would like to present the first of them - Histone template engine, which is an opensource project distributed under the Apache Software License 2.0. But first things first.

What is Histone?

Many of you use different template engines when creating web applications. The situation when you need to generate some HTML code from a data set in a certain format using a template that sets transformation rules is ubiquitous. Several years ago, template engines were actively used to generate HTML-code on the server ( Smarty , FreeMarker , Velocity ), today it is becoming more and more necessary to generate HTML-code directly in the browser. Examples of such template engines include: TrimPath templates , Mustache , Google Closure Templates , etc.

With all the variety of solutions designed to work on different platforms, there are not so many template engines that could be run simultaneously on the server and in the web browser. Each of them has its pluses and significant drawbacks. Some of these disadvantages, in our opinion, are critical, which does not allow you to fully enjoy all the benefits of a template engine that works both on the server and in a web browser.
')
The project “Histone” is an attempt to create a new open-source cross-platform template engine in implementations for Java, JavaScript and PHP. Before we started developing our own solution, we examined the advantages and disadvantages of the already existing cross-platform templating engines. We analyzed only popular template engines that are widely used for building modern web applications, and did not consider solutions that do not have at least two of the three implementations of interest (Java, JavaScript, PHP).
prosMinuses
Google Closure Templates- Template source code is compiled into JavaScript, which speeds up its processing.- To use the template, it is necessary to pre-process it using a special utility. This greatly complicates the development of browser-based applications, since when you make any changes to the template, it must be recompiled.

- Too complicated syntax of templates that will be clear to programmers, but will inevitably cause difficulties for template designers.

- A complex mechanism for creating your extensions. It does not allow to increase the functionality of the JavaScript version, which makes the extension mechanism inapplicable in the case of cross-platform use of templates.

- No PHP version
Mustache- Exists in all possible variations for a variety of programming languages.- Unconventional syntax, difficult to understand template designers.

- The template language lacks a number of important constructions, which makes the use of this template system impossible (at least, non-trivial) in the context of some projects.

- The extension mechanism is very poorly documented.

After analyzing the existing products, our team decided to create a new template engine that would have the best sides of the products listed above, as well as correct the disadvantages and shortcomings that are critical for us. As a result, a list of the main ideas that formed the basis for the development was formed:


Architecture

As part of the template engine, three interrelated components can be distinguished:


Obviously, the use of a lexical analyzer based on regular expressions and a recursive descent parser is not the most effective approach to building systems of this kind, but this is justified by the fact that our primary task was to ensure the maximum speed of the template interpretation process. Since in real conditions, working with the source code of templates will occur only during the project development process, while a web application in public access may use abstract syntax trees of templates instead of their source code, thereby completely excluding lexical analysis and parsing.

Pattern Syntax

A template is a text document with inserts of special template tags, the contents of which will be executed by the template engine during template processing. Template tags are constructs of the template language, enclosed between {{and}} characters. Fragments of a text document outside these characters will be transferred to the result of the template processing without changes.

Basics of syntax
DescriptionExample
Comments{{* comment *}}
Preventing processing{{% {{2 * 2}}%}}
Displaying property or variable values{{myValue}}
{{myProp.foo.bar}}
{{myObject ["foo"]. bar}}
Calling Functions and Macros{{loadText ("file.txt")}}
{{global.min (10, 20, 30)}}
{{myMacro ("foo", "bar")}}
Method call{{"String" .toUpperCase ()}}
{{123.isNumber ()}}

Data types
DescriptionExample
Value not defined{{undefined}}
No value{{null}}
Boolean{{true}}
{{false}}
Numbers{{ten}}
{{3.14}}
{{12E-4}}
Strings{{"String"}}
{{'string'}}
Arrays{{[1, 2, 3]}}
{{["Foo": "bar"]}}

Operations
DescriptionExample
Addition{{10 + 2}}
Subtraction{{10 - 2}}
Multiplication{{10 * 2}}
Division{{10/2}}
Remainder of the division{{10 mod 2}}
Unary minus{{-ten}}
Equally{{10 is 10}}
Not equal{{10 isNot 2}}
More{{10> 2}}
Less{{2 <10}}
Greater than or equal to{{10> = 2}}
Less than or equal to{{2 <= 10}}
Logical "OR"{{true or false}}
Logical "and"{{true and false}}
Logical "NOT"{{not true}}
Ternary operator{{true? true: false}}

Constructions
DescriptionExample
Variable declaration{{var myVar = 10}}

{{var myVar}}
...
{{/ var}}
Condition{{if true}}
...
{{elseif 42}}
...
{{else}}
...
{{/ if}}
Cycle{{for value in values}}
...
{{/ for}}

{{for key: value in values}}
...
{{/ for}}
Macros{{macro myMacro (arg1, arg2)}}
...
{{/ macro}}
Import template{{import "common.tpl"}}
Calling a function or macro{{call myProc ("arg1", "arg2")}}
arg3
{{/ call}}

Conclusion

In this article we have tried to briefly describe the main features of the new cross-platform open source template engine. You have become familiar with the internal structure and basic syntax of patterns. If you are interested in a detailed description of the technologies on the basis of which Histone works, we will be able to tell it in the next post, in which we will look at how the lexical analyzer we use works.

You can take an active part in the development of the project and apply it in your resources. Our team will be happy to answer all your questions.

Links

Source: https://habr.com/ru/post/151061/


All Articles