📜 ⬆️ ⬇️

Bicycle, or Django-like Javascript Templates

One of the components of our project Shopium.ua is the administrative interface, which is built as a fully running rich internet application client.

To draw data coming from the server to HTML, we often need a Javascript template.

There are plenty of different template engines for Javascript. Even in the ExtJS library that we use for our project, there are two of them, but their syntax and capabilities are far from ideal.
You quickly get used to good things, and we quickly got used to the Jinja2 template syntax we use to generate HTML on the server side. And they wanted the same, but with pearl buttons but for Javascript.

And formed such requirements to him:
  1. Django / jinja-like syntax
  2. compiling a template into JS code (as in Closure Templates)
  3. HTML auto shielding
  4. simplicity (development, use, implementation)

After searching the Internet and not finding anything like it, we exclaimed "Yes, developers or not, after all!", And wrote our own.

Template engine consists of a single executable file. The algorithm is very simple:
  1. Using pyparsing, parse the template source code and build the syntax tree.
  2. Recursively run through the tree, and translate it into Javascript.

The output is a .js file with the template code.

The resulting file can then be connected to your project.

The source code for the project under the MIT license is available in my BitBucket repository . There is also a brief example of using a template engine .

At the moment, all the code is very experimental, and it was written literally in one day. In our plans to bring it to a stable state and use it everywhere.

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

All Articles