spmbt / doT

The fastest + concise javascript template engine for nodejs and browsers. Partials, custom delimiters and more.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Created in search of the fastest and concise JavaScript templating function with emphasis on performance under V8 and nodejs. It shows great performance for both nodejs and browsers.

doT.js is fast, small and has no dependencies.

Features

custom delimiters
runtime evaluation
runtime interpolation
compile-time evaluation
partials support
conditionals support
array iterators (with filters since v.1.2)
object iterators with filters (since v.1.1)
encoding of HTML symbols
control whitespace - strip or preserve
streaming friendly
use it as logic-less or with logic, it is up to you

Docs, live playground and samples

New in version 1.2.1 (by author spmbt)

  • Not mandatory iterate and iterHash variables (default value are c.varname, =='it').
  • Addition for minification in Google Closure Compiler Advanced mode.
  • Page for tests in project (test/index.html) and online: https://jsfiddle.net/spmbt/v3yvpbsu/16/
  • In the test page:
    • Comparison of 2 versions (original 1.1.1 vs clone 1.2.1);
    • Handling of errors;
    • Show compiled functions;
    • benchmark with cumulative calculation of average;
    • 3 or more examples in dropdown list of examples with save of modifications in JS (no localStorage or external);
    • checked autoupdate by changes of template or data;
  • fix "strip:false" mode
  • more short inline encodeHTML() function for {{!}} templates (with encoding of HTML symbols)
  • merging of array and structure iterators

New in version 1.1.0 (by author spmbt)

###doT.js iterations by objects with conditions:

  • Iterator by object (may be just "it"):

      {{@ it.myObject : value : key}}
      	<div>{{=value }} === it.myObject[{{=it.myObject[key]}}] </div>
      {{@}}
    
  • Iterator by object with default params:

      {{@ it.myObject : value}}
      	<div>{{=value }} - value; [{{=i1}}] (default key if this iterator is first) </div>
      {{@}}
    
      {{@ it.myObject : : key}}
      	<div>{{=key}} - key; [{{=it.myObject[key]}}] (default key if this iterator is first) </div>
      {{@}}
    
  • Iterator by object with filter: if it need take part of object or filter by property of object, then, for example

      {{@ it.myObject : value : key :.hasOwnProperty(key)}}
      	<div>{{=key}} - key; [{{=it.myObject[key]}}] (default key if this iterator is first) </div>
      {{@}}
    

or any single-line or multiline expression. It means: "value.hasOwnProperty(key)".

We may to filter by any expression without context "value" if print comma:

{{@ it.myObject : value : key :, /y\\d+/.test(key)}}

(means expression: "value, /y\d+/.test(key)"). It takes all properties of it.myObject with key which have first letter "y".

####Inline one-level hash (multiline, without "}") or array (single-line, without ":") in first parameter of iterator {{@ {x1: 5, x2: 6, x3: 7, x4: it.someProperty } : value : key}}

key: {{=key}} ; value: {{=value}};
{{@}} {{@ [5, 6, 7, it.someProperty] : value : key}}
key: {{=key}} ; value: {{=value}};
{{@}} This test example in jsfiddle.net. ###Not need any default parameters in iterator by Array {{~ it.myObject : : key}}
{{=key}} - key; [{{=it.myObject[key]}}] (default key if this iterator is first)
{{}} {{ it.myObject : value}}
{{=value}} - value
{{~}} ####Tests of performance for version 1.1.0

(include previous tests and benchmarks of iterators)

doT/benchmarks/compileBench.html - full text (compile + execition)
doT/benchmarks/index.html - execution of compiled patterns only

New in version 1.0.0

####Added parameters support in partials

{{##def.macro:param:
	<div>{{=param.foo}}</div>
#}}

{{#def.macro:myvariable}}

####Node module now supports auto-compilation of dot templates from specified path

var dots = require("dot").process({ path: "./views"});

This will compile .def, .dot, .jst files found under the specified path. Details

  • It ignores sub-directories.
  • Template files can have multiple extensions at the same time.
  • Files with .def extension can be included in other files via {{#def.name}}
  • Files with .dot extension are compiled into functions with the same name and can be accessed as renderer.filename
  • Files with .jst extension are compiled into .js files. Produced .js file can be loaded as a commonJS, AMD module, or just installed into a global variable (default is set to window.render)
  • All inline defines defined in the .jst file are compiled into separate functions and are available via _render.filename.definename

Basic usage:

       var dots = require("dot").process({path: "./views"});
       dots.mytemplate({foo:"hello world"});

The above snippet will: * Compile all templates in views folder (.dot, .def, .jst) * Place .js files compiled from .jst templates into the same folder These files can be used with require, i.e. require("./views/mytemplate") * Return an object with functions compiled from .dot templates as its properties * Render mytemplate template

####CLI tool to compile dot templates into js files

./bin/dot-packer -s examples/views -d out/views

Notes

doU.js is here only so that legacy external tests do not break. Use doT.js.
doT.js with doT.templateSettings.append=false provides the same performance as doU.js.

Author

Laura Doktorova @olado
version 1.1 is modified by author spmbt

License

doT is licensed under the MIT License. (See LICENSE-DOT)

About

The fastest + concise javascript template engine for nodejs and browsers. Partials, custom delimiters and more.

License:Other


Languages

Language:JavaScript 84.0%Language:HTML 16.0%