ntpl project: simple, fast and powerfull templates for node.js (github.com/donnerjack13589/ntpl).¶ ↑
It’s easy to learn - start now! Now comes with support of Express.js!
Mustache saids that running complex.html example gives following results: Mu - 40 secs (benchmarks/million_complex.js)
On my PC - running ntpl’s: ntpl - 6 secs (benchmarks/million_complex.js)
Feel the difference?
-
ntpl have native parser written on C++, so templates are compiled very fast.
-
ntpl have many optimizations in template rendering process.
-
ntpl uses buffers
-
ntpl is extendable (ntpl.modificators)
-
ntpl can reload template on file change (watch: true)
npm install ntpl
curl github.com/donnerjack13589/ntpl/raw/master/install.sh | sh or if it dies “Permission Denied” or EACCESS error curl github.com/donnerjack13589/ntpl/raw/master/install.sh | sudo sh
git clone git@github.com:donnerjack13589/ntpl.git cd ntpl make all make test
var ntpl = require(“ntpl”).plugins(“ntpl.block”, “ntpl.filter”).ntpl; ntpl(“1 + 1 = {%= 1+1 %}”)(); ntpl({ template : “1 + 1 = {%= 1+1 %}” })(); ntpl(“filename.tpl”)(); // 1 + 1 = 2 ntpl({ template : “Hello {%= a %}!”, args: [“a”] })({a: ‘World’}); // >> Hello World! ntpl({ template: “Hello {%each a%}{%= this %}, {%/each%}world!”, args: [“a”] })({a: [‘Andy’,‘Alex’]}); // >> Hello Andy, Alex, World! ntpl({ template: “{%if godmode%}My Lord!{%else%}Who are you?{%/if%}”, args: [“godmode”] })({godmode: true}); // >> My Lord! var $ = require(“ntpl”); $.parse(“It also works!”)(); // >> It also works!
ntpl(“foo {* text here won’t be compiled or printed *} bar”)(); // >> foo bar ntpl(“{%each [1,2,3] %} ”{%= this %}“ {%/each%}?”)(); // >> “1” “2” “3” ntpl(“{%catch var a %}What’s up, dude?{%/catch%}{%= a.substr(0,9) %}?”)(); // >> What’s up? ntpl({ template: “filename.tpl”, callback: function (tpl) { console.log(tpl()); } }); // Will load “filename.tpl” in async mode
ntpl({ template: “<button>{%= value%}</button>”, args: [“value”], name: “input” }); ntpl(“input”)({value:‘Hello world!’}); // >> Button with text “Hello world!” ntpl({ template: “filename.tpl”, callback: function (tpl) { console.log(tpl()); }, watch: true }); // Will load “filename.tpl” in async mode // And watch for file changes (template will be refreshed) // P.S. Also available in a sync mode
var ntpl = require(“ntpl”).plugins(“ntpl.block”).ntpl; ntpl({ template: “Hello, {%block ‘username’%}{%/block%}!{%= message %}”, args: [“message”], name: “block-test” }); ntpl(“{%extends ‘block-test’, {message: ”How do you do?“}%}{%block ‘username’%}Admin{%/block%}”)(); // >> Hello, Admin!How do you do?
Use “set” modificator to setup options inside template
“set.html” {%set name first template %} {%set args a1, a2, a3 %} {%set some 123 %} {%set foo.bar 1,2,3 %} {%= a1 %} {%= a2 %} {%= a3 %}
Template generated from this file will have ‘first template’ name and can get ‘a1’, ‘a2’, ‘a3’ as arguments
var tpl = ntpl(“set.html”); ntpl(“first template”)({a1:1,a2:2,a3:3}); tpl.options; // {name: “first template”, args: [“a1”, “a2”, “a3”], some: “123”, foo : { bar: [“1”,“2”,“3”] } }
You can’t setup any other options now. Only ‘name’ and ‘args’ are allowed. But if you generated template with “watch=true” and then change {%set … %} in template - ‘name’ and ‘args’ will be refreshed!