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

imageProbably every web developer has heard of the Smarty template engine (php) . But since I am often fond of JavaScript, it was decided to find a similar template engine for this language, but server-side (node.js, yeah). The first links in the search engine are issued by such template engines as: ECT , JUST , Mu . All of them, of course, were not suitable for me, because were too far from Smarty syntax.

Well, if you want to, but there are no solutions, then do it. Looking for time at work and sometimes getting a “cap”, the NodeSmarty project was born , which, in the future, will partially copy the Smarty syntax .



So, the project took its roots about 4 months ago, in September 2012, and gradually took on a working form. At first, the whole template compilation algorithm was taken from Smarty (Smarty_Compiler.class.php file), some functions were not ported. In the process of writing the code, I made all kinds of code reductions in terms of simplicity, as this is “my style”. Therefore, all the huge regular expressions were replaced (and by the way, they still had a lot of unnecessary garbage in them), part of the logic in the functions.

Since the project is not run-in, it is quite clear that a sufficient number of bugs will be found in the process. I’m also sure that there will be complaints in the direction of error handling in the templates (at this stage this “function” is poorly implemented). I will only be glad to receive messages about the improvement of the code, and to the extent possible, make both corrections and new functions.

Examples of the template engine:

compiler.js:
var NodeSmarty = require('./NodeSmarty/'); 
var Template = new NodeSmarty(); 
Template
   .setTemplateDir('./templates/')
   .setCompileDir('./compile/')
   .setCacheDir('./cache/');
varArray = ['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}


Code execution speed is faster than EJS and JUST. This happens due to the fact that when the file is requested again, not a template is called, but already compiled code. But there is another plus (although it will not, but it will be in the future) - increasing the speed of execution also depends on the file system (yes, first you need to check if the templates have changed, recompile them). If you compile the files at the first request, and simply unload them at the next request, the speed will increase accordingly, and the code execution time will become even less!

Comparison of code execution speed at 1000 iterations:

speed comparison

A little about the principle of operation.

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

First, the document is parsed for the opening tag: { and the closing tag: } (although you can change it if you wish, see the documentation).
Then each tag is parsed to the main parameter and attributes. The main parameter is also processed and is divided into 2 types: command or variable. The variable differs from the command by the current dollar sign ( $ ). Both the variable and the command are processed to transform themselves into working JavaScript code. Compiled code is written to a file and eval occurstext (although there is not eval, but new Function () ).

nodesmarty.comLink to the project: NodeSmarty.com

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

nodejs Link to node.js: node.js

Also popular now: