📜 ⬆️ ⬇️

NodeSmarty new template engine for node.js (3.beta)

image Probably every web developer has heard of the Smarty template engine (php) . But since I often enjoy javascript, it was decided to find a similar template engine for this language, but server (node.js, yeah). The first links in the search engine give out such template engines such as: ECT , JUST , Mu . All of them, of course, did not fit for me, because were too far from the Smarty syntax.

Well, if you want, but there are no solutions, then do it. Looking for time at work and sometimes getting on the "header", was born the project NodeSmarty , which, in the future, will partially copy the syntax of Smarty .


')
So, the project started its roots about 4 months ago, in September 2012, and gradually acquired a working look. At the beginning, the entire algorithm for compiling templates was taken from Smarty (Smarty_Compiler.class.php file), some functions were not transferred. In the process of writing code, I did all sorts of code abbreviations in terms of simplicity, since this is “my style”. Therefore, all the huge regular expressions were replaced (and, by the way, it still was full of unnecessary garbage), part of the logic in the functions.

Since the project is unpatched, it is perfectly clear that a sufficient number of bugs will be found in the process. Also I am sure that there will be complaints in the direction of error handling in the templates (at this stage this “function” is implemented poorly). I will only be happy to receive messages on improving the code, and as far as possible I will make both corrections and new features.

Examples of template work:

compiler.js:
var NodeSmarty = require('./NodeSmarty/'); var Template = new NodeSmarty(); Template .setTemplateDir('./templates/') .setCompileDir('./compile/') .setCacheDir('./cache/'); var Array = ['val1', 'two']; Template.assign({ 'Value':'first', 'number':10, 'array':Array }); var Final = Template.fetch('template.tpl', function(data) { console.log(data); }); 


template.tpl:
 {include file="header.tpl.html"} {$Value} {$number*10/20} {if !$Value} //... {elseif $number = $Value} //... {else} //... {/if} {* comment *} {literal} for(var di=0; di < 10; di++) { console.log(di); } {/literal} {foreach from=$array item=Foo key=Key} Item: {$Foo} key: {$Key} {/foreach} 


The speed of code execution is higher than that of EJS and JUST. This happens because when you re-request a file, it is not the template that is called, but already the compiled code. But there is one more plus (although it is not there, but it will be in the future) - increasing the speed of execution also depends on the file system (yes, first you need to check whether the templates have changed, to recompile them). If you compile the files at the first request, and then simply unload them from memory, then the speed will increase accordingly, and the code execution time will be even smaller!

Comparing the speed of code execution at 1000 iterations:

speed comparison

A little bit about the principle of work.

If the template is changed before the file is compiled, the library simply loads the compiled file and inserts values ​​into it. If the template is changed later, the following instructions are followed:

First of all, the document is parsed for the presence of the opening tag: { and the closing tag: } (although you can change it at will, see the documentation).
Then each tag is parsed to the main parameter and attributes. The main parameter is also processed and divided into 2 types: command or variable. The variable is different from the command with the leading dollar sign ( $ ). Both the variable and the command are processed to convert themselves into working JavaScript code. The compiled code is written to a file and eval text occurs (although there is not eval, but new Function () ).

nodesmarty.com Link to the project: NodeSmarty.com

github.com GitHub.com Link: github.com/lampaa/NodeSmarty

nodejs Link to node.js: node.js

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


All Articles