Bicycle, or Django-like Javascript Templates
One of the components of our Shopium.ua project is the administrative interface, which is built as fully executing on the client rich internet application.
To draw data coming from the server in HTML, we often need a Javascript template engine.
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 the good, and we quickly get used to the Jinja2 template syntax that we use to generate server-side HTML. And they wanted the same,but with pearl buttons but for Javascript.
And formed such requirements for him:
After searching the Internet and not finding anything like it, we exclaimed “Yes, we are developers or not, in the end!”, And wrote our own.
The template engine consists of one executable file. The operation algorithm is very simple:
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 the use of a template engine .
At the moment, all the code is very experimental, and was written in just one day. Our plans are to bring it to a stable state and use it everywhere.
To draw data coming from the server in HTML, we often need a Javascript template engine.
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 the good, and we quickly get used to the Jinja2 template syntax that we use to generate server-side HTML. And they wanted the same,
And formed such requirements for him:
- Django / Jinja-like syntax
- compiling a template in JS code (as in Closure Templates)
- HTML auto-escaping
- simplicity (development, use, implementation)
After searching the Internet and not finding anything like it, we exclaimed “Yes, we are developers or not, in the end!”, And wrote our own.
The template engine consists of one executable file. The operation algorithm is very simple:
- Using pyparsing, we parse the source code of the template, and build a syntax tree.
- We 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 the use of a template engine .
At the moment, all the code is very experimental, and was written in just one day. Our plans are to bring it to a stable state and use it everywhere.