http://github.com/jquery/jquery-tmpl
http://api.jquery.com/category/plugins/templates/
Note: currently not implemented: wrap tag and tmplItem method.
http://docs.djangoproject.com/en/dev/topics/templates/
- no program logic in templates
- no embeded script language like in ejs
- this is evil because it enables program logic in templates
- bad usability
- because of the "var" problem in javascript - very evil
- jquery tmpl plugin conform
- extendable - you can implement new statements
- html escaping per default
- simple syntax
- tiny and fast
npm install jqtpl
$ node test/test.js
var jqtpl = require( "jqtpl" );
app.set( "view engine", "html" );
app.register( ".html", require( "jqtpl" ) );
Following options are supported by render method
localsLocal variables objectcacheCompiled functions are cached, requiresfilenamefilenameUsed bycacheto key cachesscopeFunction execution contextdebugOutput generated function body
jqtpl.render('your template', {
locals: {
// your data here
},
cache: true, // default
filename: 'file-name',
scope: {} // default to {}
debug: false // will output generated function to the console, false is default
});
<div>${a}</div>
jqtpl.tmpl( tpl, {a:123});
<div>123</div>
<div>${a}</div>
jqtpl.tmpl( tpl, [{a:1},{a:2},{a:3}]);
<div>1</div><div>2</div><div>3</div>
<div>${a}</div>
jqtpl.tmpl( tpl, {
a:function() {
return 1 + 5;
}
});
<div>6</div>
{{if a == 6}}
<div>${a}</div>
{{else a == 5}}
<div>5</div>
{{else}}
<div>a is not 6 and not 5</div>
{{/if}}
jqtpl.tmpl( tpl, {a:6});
<div>6</div>
jqtpl.tmpl( tpl, {a:5});
<div>a is not 6</div>
{{each(i, name) names}}
<div>${i}.${name}</div>
{{/each}}
or {{each names}}
${$index}.${$value}
{{/each}}
jqtpl.tmpl( tpl, {names: ["A", "B"]});
<div>0.A</div><div>1.B</div>
<div>{{html a}}</div>
jqtpl.tmpl( tpl, {a:'<div id="123">2</div>'});
<div id="123">2</div>
Named templates - there is a way to precompile the template using a string, so you can render this template later using its name
<div>${a}</div>
// precompile an cache it
jte.template( "templateName", tpl );
jqtpl.tmpl( "templateName", {a:1} );
// you can also delete the template from cache
delete jte.template["templateName"];
<div>1</div>
- $data - data object passed to render method
- $item - contains $data via $item.data as well as user options - an optional map of user-defined key-value pairs.
<div>${ $item.someMethod() }</div>
jqtpl.tmpl( tpl, {a:1}, {
someMethod: function(){ return 1; }
});
<div>1</div>
<div>${a}</div>
// precompile an cache it
jte.template( "templateName", tpl );
jqtpl.tmpl( "templateName", {a:1} );
// you can also delete the template from cache
delete jte.template["templateName"];
<div>1</div>
<div>{{! its a comment}}</div>
jqtpl.tmpl( tpl );
<div></div>
There are 2 ways supported. Note: passing json object with 2 curly brackets without any separation will break the template engine: {{tmpl({a: {b: 1}}) "mypartial"}}
// The first way is jquery-tmpl compatible and preferred:
<div>{{tmpl({name: "Test"}) "mypartial"}}</div>
// The second way is provided by express and is not jquery-tmpl compatible:
<div>{{html partial("mypartial", {name: "Test"})}}</div>
${name}
jqtpl.tmpl( tpl );
<div>Test</div>