Simple and intuitive, powerful and extensible, lightning fast
For templated content in the browser or on Node.js (with Express 4, Hapi and Browserify integration)
JsRender is a light-weight but powerful templating engine, highly extensible, and optimized for high-performance rendering, without DOM dependency. It is designed for use in the browser or on Node.js, with or without jQuery.
JsRender and JsViews together provide the next-generation implementation of the official jQuery plugins JQuery Templates, and JQuery Data Link – and supersede those libraries.
Documentation, downloads, samples and API docs and tutorials are available on the www.jsviews.com website.
The content of this ReadMe is available also as a JsRender Quickstart.
JsRender is used for data-driven rendering of templates to strings, ready for insertion in the DOM.
It is also used by the JsViews platform, which adds data binding to JsRender templates, and provides a fully-fledged MVVM platform for easily creating interactive data-driven single page apps and websites.
jsrender.js is available from downloads on the jsviews.com site.
CDN delivery is available from the jsdelivr CDN at jsdelivr.com/package/npm/jsrender, and from the cdnjs CDN at cdnjs.com/libraries/jsrender. Alternatively:
$ bower install jsrender
When jQuery is present, JsRender loads as a jQuery plugin and adds $.views
, $.templates
and $.render
to the jQuery namespace object, $
(or window.jQuery
).
Example HTML page: JsRender with jQuery
When jQuery is not present, JsRender provides its own global namespace object: jsrender
(or window.jsrender
)
The jsrender
namespace provides the same methods/APIs as with jQuery, so if jQuery is not present you can still use all the API examples, by simply writing:
var $ = window.jsrender;
// Now use code as in samples/examples, with $.views... $.templates... $.render...
(Note: If jQuery is not loaded, then passing a jQuery selector to $.templates()
will only work for the ID selector)
Example HTML page: JsRender without jQuery
JsRender can be used to render templates on the server (using Node.js) as well as in the browser. JsRender on Node.js has all the features and APIs of JsRender in the browser, plus some additional ones specific to Node.js.
It also provides built-in Express, Hapi and Browserify integration – which makes it easy to register templates as simple .html
files on the file system, and then load and render them either server-side, client-side or both.
Learn more: JsRender Node.js Quickstart and JsRender APIs for Node.js.
Code samples: See JsRender Node Starter for running code examples of Node.js scenarios, including with Express, Hapi and Browserify.
From a string:
var tmpl = $.templates("Name: ");
From a template declared as markup in a script block:
<script id="myTemplate" type="text/x-jsrender">
Name:
</script>
then, somewhere in your script:
var tmpl = $.templates("#myTemplate"); // Pass in a jQuery selector for the script block
On Node.js, from an .html file containing the template markup:
var $ = require('jsrender'); // returns the jsrender namespace object
var tmpl = $.templates("./templates/myTemplate.html");
tmpl.render(object)
(or shortcut form: tmpl(object)
) renders the template with the object as data context.
var tmpl = $.templates(" Name: <br/> ");
var person = {name: "Jim"};
// Render template for person object
var html = tmpl.render(person); // ready for insertion, e.g $("#result").html(html);
// result: "Name: Jim<br/> "
tmpl.render(array)
(or tmpl(array)
) renders the template once for each item in the array.
var people = [{name: "Jim"}, {name: "Pedro"}];
// Render template for people array
var html = tmpl.render(people); // ready for insertion...
// result: "Name: Jim<br/> Name: Pedro<br/> "
// Register named template - "myTmpl1
$.templates("myTmpl1", "Name: <br/> ");
var person = {name: "Jim"};
// Render named template
var html = $.templates.myTmpl1(person);
// Alternative syntax: var html = $.render.myTmpl1(person);
// result: "Name: Jim<br/> "
All tags other than
behave as block tags
A particular case of self-closing syntax is when any block tag uses the named parameter tmpl=...
to reference an external template, which then replaces what would have been the block content:
tmpl=...
parameter mentioned aboveaddress.street
or from richer expressions such as product.quantity * 3.1 / 4.5
, or name.toUpperCase()
`` inserts the value of the path or expression.
var data = {address: {street: "Main Street"} };
var tmpl = $.templates("<b>Street:</b> ");
var html = tmpl.render(data);
// result: "<b>Street:</b> Main Street"
`` inserts the HTML-encoded value of the path or expression.
var data = {condition: "a < b"};
var tmpl = $.templates("<b>Formula:</b> ");
var html = tmpl.render(data);
// result: "<b>Formula:</b> a < b"
...
evaluates the block content against a specified/modified data context.
`` evaluates the specified template against an (optionally modified) context, and inserts the result. (Template composition).
var data = {name: "Jim", address: {street: "Main Street"} };
// Register two named templates
$.templates({
streetTmpl: "<i></i>",
addressTmpl: "'s address is ."
});
// Render outer template
var html = $.templates.addressTmpl(data);
// result: "Jim's address is <i>Main Street</i>"
...
evaluates the block content against a specified data context. If the new data context is an array, it iterates over the array, renders the block content with each data item as context, and concatenates the result.
`` evaluates the specified template against a data context. If the new data context is an array, it iterates over the array, renders the template with each data item as context, and concatenates the result.
<script id="peopleTmpl" type="text/x-jsrender">
<ul>
<li>Name: </li>
</ul>
</script>
var data = {people: [{name: "Jim"}, {name: "Pedro"}] };
var tmpl = $.templates("#peopleTmpl");
var html = tmpl.render(data);
// result: "<ul> <li>Name: Jim</li> <li>Name: Pedro</li> </ul>"
...
or `` iterates over the properties of the object returned by the path or expression, and renders the content/template once for each property - using as data context: {key: propertyName, prop: propertyValue}
.
<script id="personTmpl" type="text/x-jsrender">
<ul>
<li>: </li>
</ul>
</script>
var data = {person: {first: "Jim", last: "Varsov"} };
var tmpl = $.templates("#personTmpl");
var html = tmpl.render(data);
// result: "<ul> <li>first: Jim</li> <li>last: Varsov</li> </ul>"
...
or `` renders the content/template only if the evaluated path or expression is ‘truthy’.
.........
behaves as ‘if’ - ‘else if’ - ‘else’ and renders each block based on the conditions.
<script id="personTmpl" type="text/x-jsrender">
Nickname:
Name:
No name provided
</script>
var data = {nickname: "Jim", name: "James"};
var tmpl = $.templates("#personTmpl");
var html = tmpl.render(data);
// result: "Nickname: Jim"
For details on all the above built-in tags, as well as comment tags __ and allow code tags _ and _, see the tags documentation on jsviews.com.
Creating your own custom tags is easy. You can provide an object, with render method, template, event handlers, etc. See samples here and here on jsviews.com. But for simple tags, you may only need a simple render function, or a template string.
For example the two following definitions for a `` tag provide equivalent behavior:
As a render function:
$.views.tags("fullName", function(val) {
return val.first + " " + val.last;
});
Or as a template string:
$.views.tags("fullName", "page{"permalink"=>"//", "layout"=>"home", "title"=>"JsRender: best-of-breed templating", "content"=>"## JsRender: best-of-breed templating\n\n[](https://www.npmjs.com/package/jsrender)\n[](https://cdnjs.com/libraries/jsrender)\n\n*Simple and intuitive, powerful and extensible, lightning fast*\n\n*For templated content in the browser or on Node.js (with Express 4, Hapi and Browserify integration)*\n\n**JsRender** is a light-weight but powerful templating engine, highly extensible, and optimized for high-performance rendering, without DOM dependency. It is designed for use in the browser or on Node.js, with or without jQuery.\n\n**[JsRender](https://github.com/BorisMoore/jsrender)** and **[JsViews](https://github.com/BorisMoore/jsviews)** together provide the next-generation implementation of the official jQuery plugins *[JQuery Templates](https://github.com/BorisMoore/jquery-tmpl)*, and *[JQuery Data Link](https://github.com/BorisMoore/jquery-datalink)* -- and supersede those libraries.\n\n### Documentation and downloads\n\n**[Documentation](http://www.jsviews.com)**, **[downloads](http://www.jsviews.com/#download)**, **[samples](http://www.jsviews.com/#samples)** and **[API docs and tutorials](http://www.jsviews.com/#jsrapi)** are available on the **[www.jsviews.com website](http://www.jsviews.com/#jsrender)**.\n\nThe content of this ***ReadMe*** is available also as a *[JsRender Quickstart](http://www.jsviews.com/#jsr-quickstart)*.\n\n### JsRender and JsViews\n\nJsRender is used for data-driven rendering of templates to strings, ready for insertion in the DOM.\n\nIt is also used by the *[JsViews](http://www.jsviews.com/#jsviews)* platform, which adds data binding to JsRender templates, and provides a fully-fledged MVVM platform for easily creating interactive data-driven single page apps and websites.\n\n## JsRender installation\n\n*jsrender.js* is available from [downloads](http://www.jsviews.com/#download) on the jsviews.com site. \n\n*CDN delivery* is available from the ***[jsdelivr](https://jsdelivr.com)*** CDN at [jsdelivr.com/package/npm/jsrender](https://jsdelivr.com/package/npm/jsrender?tab=files), and from the ***[cdnjs](https://cdnjs.com)*** CDN at [cdnjs.com/libraries/jsrender](https://cdnjs.com/libraries/jsrender).\nAlternatively:\n- It can be installed with **[Bower](http://bower.io/search/?q=jsrender)**, using `$ bower install jsrender` \n- It can be loaded using an *AMD script loader*, such as RequireJS\n- For installation using *Node.js* (*npm*) see *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)*\n- (For browser loading using *Browserify* or *webpack* - see *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)*, *[JsRender as a Browserify module](http://www.jsviews.com/#node/browserify@jsrender)* and *[JsRender as a webpack module](http://www.jsviews.com/#node/webpack@jsrender)*)\n\n#### Using JsRender with jQuery\n\nWhen jQuery is present, JsRender loads as a jQuery plugin and adds `$.views`, `$.templates` and `$.render` to the jQuery namespace object, `$` (or `window.jQuery`).\n\n*Example HTML page:* [JsRender with jQuery](http://www.jsviews.com/#download/pages-jsr-jq)\n\n#### Using JsRender without jQuery\n\nWhen jQuery is not present, JsRender provides its own global namespace object: `jsrender` (or `window.jsrender`)\n\nThe `jsrender` namespace provides the same methods/APIs as with jQuery, so if jQuery is not present you can still use all the API examples, by simply writing:\n\n```js\nvar $ = window.jsrender;\n\n// Now use code as in samples/examples, with $.views... $.templates... $.render...\n```\n(*Note:* If jQuery is not loaded, then [passing a jQuery selector](http://www.jsviews.com/#compiletmpl@fromscriptblock) to `$.templates()` will only work for the *ID selector*)\n\n*Example HTML page:* [JsRender without jQuery](http://www.jsviews.com/#download/pages-jsr)\n\n#### JsRender on Node.js\n\nJsRender can be used to render templates on the server (using Node.js) as well as in the browser. JsRender on Node.js has all the features and APIs of JsRender in the browser, plus some additional ones specific to Node.js.\n\nIt also provides built-in *Express*, *Hapi* and *Browserify* integration -- which makes it easy to register templates as simple `.html` files on the file system, and then load and render them either server-side, client-side or both.\n\n**Learn more:** *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)* and *[JsRender APIs for Node.js](http://www.jsviews.com/#jsrnode)*.\n\n**Code samples:** See *[JsRender Node Starter](https://github.com/BorisMoore/jsrender-node-starter)* for running code examples of Node.js scenarios, including with *Express*, *Hapi* and *Browserify*.\n\n## JsRender usage\n\n### _Define a template_\n\nFrom a string:\n\n```js\nvar tmpl = $.templates(\"Name: {{:name}}\");\n```\n\nFrom a template declared as markup in a script block:\n\n```html\n<script id=\"myTemplate\" type=\"text/x-jsrender\">\nName: {{:name}}\n</script>\n```\n\nthen, somewhere in your script:\n\n```js\nvar tmpl = $.templates(\"#myTemplate\"); // Pass in a jQuery selector for the script block\n```\n\nOn Node.js, [from an .html file](https://www.jsviews.com/#jsr-node-quickstart@htmlfiles) containing the template markup: \n\n```js\nvar $ = require('jsrender'); // returns the jsrender namespace object\nvar tmpl = $.templates(\"./templates/myTemplate.html\");\n```\n\n[Learn more...](http://www.jsviews.com/#d.templates)\n\n### _Render a template_\n\n`tmpl.render(object)` (or shortcut form: `tmpl(object)`) renders the template with the object as data context.\n\n```js\nvar tmpl = $.templates(\" Name: {{:name}}<br/> \");\n\nvar person = {name: \"Jim\"};\n\n// Render template for person object\nvar html = tmpl.render(person); // ready for insertion, e.g $(\"#result\").html(html);\n\n// result: \"Name: Jim<br/> \"\n```\n\n`tmpl.render(array)` (or `tmpl(array)`) renders the template once for each item in the array.\n\n```js\nvar people = [{name: \"Jim\"}, {name: \"Pedro\"}];\n\n// Render template for people array\nvar html = tmpl.render(people); // ready for insertion...\n\n// result: \"Name: Jim<br/> Name: Pedro<br/> \"\n```\n\n[Learn more...](http://www.jsviews.com/#rendertmpl)\n\n### _Register a named template - and render it_\n\n```js\n// Register named template - \"myTmpl1\n$.templates(\"myTmpl1\", \"Name: {{:name}}<br/> \");\n\nvar person = {name: \"Jim\"};\n\n// Render named template\nvar html = $.templates.myTmpl1(person);\n\n// Alternative syntax: var html = $.render.myTmpl1(person);\n\n// result: \"Name: Jim<br/> \"\n```\n\n[Learn more...](http://www.jsviews.com/#rendertmpl)\n\n### _Template tags_\n\n#### Template tag syntax\n\n- All tags other than `{{: ...}}` `{{> ...}}` `{{* ...}}` `{{!-- --}}` behave as *block tags*\n\n- Block tags can have content, unless they use the self-closing syntax:\n - *Block tag - with content:* `{{someTag ...}} content {{/someTag}}`\n - *Self-closing tag - no content (empty):* `{{someTag .../}}`\n\n- A particular case of self-closing syntax is when any block tag uses the named parameter `tmpl=...` to reference an external template, which then replaces what would have been the block content:\n \n - *Self-closing block tag referencing an external template:* `{{someTag ... tmpl=.../}}`\n (This lets you do [template composition](http://www.jsviews.com/#tagsyntax@composition). See [example](http://www.jsviews.com/#samples/jsr/composition/tmpl).)\n\n- Tags can take both unnamed arguments and named parameters:\n - `{{someTag argument1 param1=...}} content {{/someTag}}`\n - an example of a named parameter is the `tmpl=...` parameter mentioned above\n - arguments and named parameters can be assigned values from simple data-paths such as `address.street` or from richer expressions such as `product.quantity * 3.1 / 4.5`, or `name.toUpperCase()`\n\n[Learn more...](http://www.jsviews.com/#tagsyntax)\n\n#### Built-in tags\n\n#### _{{: ...}}_ (Evaluate)\n\n`{{: pathOrExpr}}` inserts the value of the path or expression.\n\n```js\nvar data = {address: {street: \"Main Street\"} };\nvar tmpl = $.templates(\"<b>Street:</b> {{:address.street}}\");\nvar html = tmpl.render(data);\n\n// result: \"<b>Street:</b> Main Street\"\n```\n\n[Learn more...](http://www.jsviews.com/#assigntag)\n\n#### _{{> ...}}_ (HTML-encode) \n\n`{{> pathOrExpr}}` inserts the *HTML-encoded* value of the path or expression.\n\n```js\nvar data = {condition: \"a < b\"};\nvar tmpl = $.templates(\"<b>Formula:</b> {{>condition}}\");\nvar html = tmpl.render(data);\n\n// result: \"<b>Formula:</b> a < b\"\n```\n\n[Learn more...](http://www.jsviews.com/#htmltag)\n\n#### _{{include ...}}_ (Template composition - partials)\n\n`{{include pathOrExpr}}...{{/include}}`evaluates the block content against a specified/modified data context.\n\n`{{include ... tmpl=.../}}` evaluates the specified template against an (optionally modified) context, and inserts the result. (Template composition).\n\n```js\nvar data = {name: \"Jim\", address: {street: \"Main Street\"} };\n\n// Register two named templates\n$.templates({\n streetTmpl: \"<i>{{:street}}</i>\",\n addressTmpl: \"{{:name}}'s address is {{include address tmpl='streetTmpl'/}}.\"\n});\n\n// Render outer template\nvar html = $.templates.addressTmpl(data);\n\n// result: \"Jim's address is <i>Main Street</i>\"\n```\n\n[Learn more...](http://www.jsviews.com/#includetag)\n\n#### _{{for ...}}_ (Template composition, with iteration over arrays)\n\n`{{for pathOrExpr}}...{{/for}}`evaluates the block content against a specified data context. If the new data context is an array, it iterates over the array, renders the block content with each data item as context, and concatenates the result.\n\n`{{for pathOrExpr tmpl=.../}}` evaluates the specified template against a data context. If the new data context is an array, it iterates over the array, renders the template with each data item as context, and concatenates the result.\n\n```html\n<script id=\"peopleTmpl\" type=\"text/x-jsrender\">\n <ul>{{for people}}\n <li>Name: {{:name}}</li>\n {{/for}}</ul>\n</script>\n```\n\n```js\nvar data = {people: [{name: \"Jim\"}, {name: \"Pedro\"}] };\nvar tmpl = $.templates(\"#peopleTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"<ul> <li>Name: Jim</li> <li>Name: Pedro</li> </ul>\"\n```\n\n[Learn more...](http://www.jsviews.com/#fortag)\n\n#### _{{props ...}}_ (Iteration over properties of an object)\n\n`{{props pathOrExpr}}...{{/prop}}` or `{{props pathOrExpr tmpl=.../}}` iterates over the properties of the object returned by the path or expression, and renders the content/template once for each property - using as data context: `{key: propertyName, prop: propertyValue}`.\n\n```html\n<script id=\"personTmpl\" type=\"text/x-jsrender\">\n <ul>{{props person}}\n <li>{{:key}}: {{:prop}}</li>\n {{/props}}</ul>\n</script>\n```\n\n```js\nvar data = {person: {first: \"Jim\", last: \"Varsov\"} };\nvar tmpl = $.templates(\"#personTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"<ul> <li>first: Jim</li> <li>last: Varsov</li> </ul>\"\n```\n\n[Learn more...](http://www.jsviews.com/#propstag)\n\n#### _{{if ...}}_ (Conditional inclusion)\n\n`{{if pathOrExpr}}...{{/if}}` or `{{if pathOrExpr tmpl=.../}}` renders the content/template only if the evaluated path or expression is 'truthy'.\n\n`{{if pathOrExpr}}...{{else pathOrExpr2}}...{{else}}...{{/if}}` behaves as '*if' - 'else if' - 'else'* and renders each block based on the conditions.\n\n```html\n<script id=\"personTmpl\" type=\"text/x-jsrender\">\n {{if nickname}}\n Nickname: {{:nickname}}\n {{else name}}\n Name: {{:name}}\n {{else}}\n No name provided\n {{/if}}\n</script>\n```\n\n```js\nvar data = {nickname: \"Jim\", name: \"James\"};\nvar tmpl = $.templates(\"#personTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"Nickname: Jim\"\n```\n\n[Learn more...](http://www.jsviews.com/#iftag)\n\n#### Other built-in tags\n\nFor details on all the above built-in tags, as well as *[comment tags](http://www.jsviews.com/#commenttag)* _{{!-- ... --}}_ and *[allow code tags](http://www.jsviews.com/#allowcodetag)* _{{\\* ... }} and {{\\*: ...}}_, see the [tags documentation](http://www.jsviews.com/#jsrtags) on jsviews.com.\n\n#### Custom tags\n\nCreating your own custom tags is easy. You can provide an object, with render method, template, event handlers, etc. See samples [here](http://www.jsviews.com/#samples/jsr/tags) and [here](http://www.jsviews.com/#samples/tag-controls) on jsviews.com. But for simple tags, you may only need a simple render function, or a template string. \n\nFor example the two following definitions for a `{{fullName/}}` tag provide equivalent behavior:\n\nAs a render function:\n\n```js\n$.views.tags(\"fullName\", function(val) {\n return val.first + \" \" + val.last;\n});\n```\n\nOr as a template string:\n\n```js\n$.views.tags(\"fullName\", \"{{:first}} {{:last}}\");\n```\n\nEither way, the result will be as follows:\n\n```js\nvar tmpl = $.templates(\"{{fullName person/}}\");\nvar data = {person: {first: \"Jim\", last: \"Varsov\"}};\nvar html = tmpl.render(data);\n\n// result: \"Jim Varsov\"\n```\n\n### _Helpers_\n\nFor details on helpers, see the [Helpers](http://www.jsviews.com/#helpers) documentation topic on jsviews.com.\n\nHere is a simple example. Two helpers - a function, and a string:\n\n```js\nvar myHelpers = {\n upper: function(val) { return val.toUpperCase(); },\n title: \"Sir\"\n};\n```\n\nAccess the helpers using the `~myhelper` syntax:\n\n```js\nvar tmpl = $.templates(\"{{:~title}} {{:first}} {{:~upper(last)}}\");\n```\n\nWe can pass the helpers in with the `render()` method\n\n```js\nvar data = {first: \"Jim\", last: \"Varsov\"};\n\nvar html = tmpl.render(data, myHelpers);\n\n// result: \"Sir Jim VARSOV\"\n```\n\nOr we can register helpers globally:\n\n```js\n$.views.helpers(myHelpers);\n\nvar data = {first: \"Jim\", last: \"Varsov\"};\nvar html = tmpl.render(data);\n\n// result: \"Sir Jim VARSOV\"\n```\n\n[Learn more...](http://www.jsviews.com/#helpers)\n\n### _Converters_\n\nConverters are used with the `{{:...}}` tag, using the syntax `{{mycvtr: ...}}}`.\n\nExample - an *upper* converter, to convert to upper case: \n\n```js\n$.views.converters(\"upper\", function(val) { return val.toUpperCase(); });\n\nvar tmpl = $.templates(\"{{:first}} {{upper:last}}\");\nvar data = {first: \"Jim\", last: \"Varsov\"};\nvar html = tmpl.render(data);\n\n// result: \"Jim VARSOV\"\n```\n\n[Learn more...](http://www.jsviews.com/#converters)\n\n### _Logic and expressions_\n\nJsRender supports rich expressions and logic, but at the same time encapsulates templates to prevent random access to globals. If you want to provide access to global variables within a template, you have to pass them in as data or as helpers.\n\nYou can assign rich expressions to any template arguments or parameters, as in:\n\n`{{:person.nickname ? \"Nickname: \" + person.nickname : \"(has no nickname)\"}}`\n\nor\n\n```html\n{{if ~limits.maxVal > (product.price*100 - discount)/rate}}\n ...\n{{else ~limits.minVal < product.price}}\n ... \n{{else}}\n ... \n{{/if}}\n```\n\n### _Documentation and APIs_\n\nSee the [www.jsviews.com](http://www.jsviews.com) site, including the *[JsRender Quickstart](http://www.jsviews.com/#jsr-quickstart)* and [JsRender APIs](http://www.jsviews.com/#jsrapi) topics.\n\n### _Demos_\n\nDemos and samples can be found at [www.jsviews.com/#samples](http://www.jsviews.com/#samples/jsr), and throughout the [API documentation](http://www.jsviews.com/#jsrapi).\n\n(See also the [demos](https://github.com/BorisMoore/jsrender/tree/master/demos) folder of the GitHub repository - available [here](http://borismoore.github.io/jsrender/demos/index.html) as live samples).\n", "dir"=>"/", "name"=>"README.md", "path"=>"README.md", "url"=>"/"} ");
Either way, the result will be as follows:
var tmpl = $.templates("");
var data = {person: {first: "Jim", last: "Varsov"}};
var html = tmpl.render(data);
// result: "Jim Varsov"
For details on helpers, see the Helpers documentation topic on jsviews.com.
Here is a simple example. Two helpers - a function, and a string:
var myHelpers = {
upper: function(val) { return val.toUpperCase(); },
title: "Sir"
};
Access the helpers using the ~myhelper
syntax:
var tmpl = $.templates(" page{"permalink"=>"//", "layout"=>"home", "title"=>"JsRender: best-of-breed templating", "content"=>"## JsRender: best-of-breed templating\n\n[](https://www.npmjs.com/package/jsrender)\n[](https://cdnjs.com/libraries/jsrender)\n\n*Simple and intuitive, powerful and extensible, lightning fast*\n\n*For templated content in the browser or on Node.js (with Express 4, Hapi and Browserify integration)*\n\n**JsRender** is a light-weight but powerful templating engine, highly extensible, and optimized for high-performance rendering, without DOM dependency. It is designed for use in the browser or on Node.js, with or without jQuery.\n\n**[JsRender](https://github.com/BorisMoore/jsrender)** and **[JsViews](https://github.com/BorisMoore/jsviews)** together provide the next-generation implementation of the official jQuery plugins *[JQuery Templates](https://github.com/BorisMoore/jquery-tmpl)*, and *[JQuery Data Link](https://github.com/BorisMoore/jquery-datalink)* -- and supersede those libraries.\n\n### Documentation and downloads\n\n**[Documentation](http://www.jsviews.com)**, **[downloads](http://www.jsviews.com/#download)**, **[samples](http://www.jsviews.com/#samples)** and **[API docs and tutorials](http://www.jsviews.com/#jsrapi)** are available on the **[www.jsviews.com website](http://www.jsviews.com/#jsrender)**.\n\nThe content of this ***ReadMe*** is available also as a *[JsRender Quickstart](http://www.jsviews.com/#jsr-quickstart)*.\n\n### JsRender and JsViews\n\nJsRender is used for data-driven rendering of templates to strings, ready for insertion in the DOM.\n\nIt is also used by the *[JsViews](http://www.jsviews.com/#jsviews)* platform, which adds data binding to JsRender templates, and provides a fully-fledged MVVM platform for easily creating interactive data-driven single page apps and websites.\n\n## JsRender installation\n\n*jsrender.js* is available from [downloads](http://www.jsviews.com/#download) on the jsviews.com site. \n\n*CDN delivery* is available from the ***[jsdelivr](https://jsdelivr.com)*** CDN at [jsdelivr.com/package/npm/jsrender](https://jsdelivr.com/package/npm/jsrender?tab=files), and from the ***[cdnjs](https://cdnjs.com)*** CDN at [cdnjs.com/libraries/jsrender](https://cdnjs.com/libraries/jsrender).\nAlternatively:\n- It can be installed with **[Bower](http://bower.io/search/?q=jsrender)**, using `$ bower install jsrender` \n- It can be loaded using an *AMD script loader*, such as RequireJS\n- For installation using *Node.js* (*npm*) see *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)*\n- (For browser loading using *Browserify* or *webpack* - see *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)*, *[JsRender as a Browserify module](http://www.jsviews.com/#node/browserify@jsrender)* and *[JsRender as a webpack module](http://www.jsviews.com/#node/webpack@jsrender)*)\n\n#### Using JsRender with jQuery\n\nWhen jQuery is present, JsRender loads as a jQuery plugin and adds `$.views`, `$.templates` and `$.render` to the jQuery namespace object, `$` (or `window.jQuery`).\n\n*Example HTML page:* [JsRender with jQuery](http://www.jsviews.com/#download/pages-jsr-jq)\n\n#### Using JsRender without jQuery\n\nWhen jQuery is not present, JsRender provides its own global namespace object: `jsrender` (or `window.jsrender`)\n\nThe `jsrender` namespace provides the same methods/APIs as with jQuery, so if jQuery is not present you can still use all the API examples, by simply writing:\n\n```js\nvar $ = window.jsrender;\n\n// Now use code as in samples/examples, with $.views... $.templates... $.render...\n```\n(*Note:* If jQuery is not loaded, then [passing a jQuery selector](http://www.jsviews.com/#compiletmpl@fromscriptblock) to `$.templates()` will only work for the *ID selector*)\n\n*Example HTML page:* [JsRender without jQuery](http://www.jsviews.com/#download/pages-jsr)\n\n#### JsRender on Node.js\n\nJsRender can be used to render templates on the server (using Node.js) as well as in the browser. JsRender on Node.js has all the features and APIs of JsRender in the browser, plus some additional ones specific to Node.js.\n\nIt also provides built-in *Express*, *Hapi* and *Browserify* integration -- which makes it easy to register templates as simple `.html` files on the file system, and then load and render them either server-side, client-side or both.\n\n**Learn more:** *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)* and *[JsRender APIs for Node.js](http://www.jsviews.com/#jsrnode)*.\n\n**Code samples:** See *[JsRender Node Starter](https://github.com/BorisMoore/jsrender-node-starter)* for running code examples of Node.js scenarios, including with *Express*, *Hapi* and *Browserify*.\n\n## JsRender usage\n\n### _Define a template_\n\nFrom a string:\n\n```js\nvar tmpl = $.templates(\"Name: {{:name}}\");\n```\n\nFrom a template declared as markup in a script block:\n\n```html\n<script id=\"myTemplate\" type=\"text/x-jsrender\">\nName: {{:name}}\n</script>\n```\n\nthen, somewhere in your script:\n\n```js\nvar tmpl = $.templates(\"#myTemplate\"); // Pass in a jQuery selector for the script block\n```\n\nOn Node.js, [from an .html file](https://www.jsviews.com/#jsr-node-quickstart@htmlfiles) containing the template markup: \n\n```js\nvar $ = require('jsrender'); // returns the jsrender namespace object\nvar tmpl = $.templates(\"./templates/myTemplate.html\");\n```\n\n[Learn more...](http://www.jsviews.com/#d.templates)\n\n### _Render a template_\n\n`tmpl.render(object)` (or shortcut form: `tmpl(object)`) renders the template with the object as data context.\n\n```js\nvar tmpl = $.templates(\" Name: {{:name}}<br/> \");\n\nvar person = {name: \"Jim\"};\n\n// Render template for person object\nvar html = tmpl.render(person); // ready for insertion, e.g $(\"#result\").html(html);\n\n// result: \"Name: Jim<br/> \"\n```\n\n`tmpl.render(array)` (or `tmpl(array)`) renders the template once for each item in the array.\n\n```js\nvar people = [{name: \"Jim\"}, {name: \"Pedro\"}];\n\n// Render template for people array\nvar html = tmpl.render(people); // ready for insertion...\n\n// result: \"Name: Jim<br/> Name: Pedro<br/> \"\n```\n\n[Learn more...](http://www.jsviews.com/#rendertmpl)\n\n### _Register a named template - and render it_\n\n```js\n// Register named template - \"myTmpl1\n$.templates(\"myTmpl1\", \"Name: {{:name}}<br/> \");\n\nvar person = {name: \"Jim\"};\n\n// Render named template\nvar html = $.templates.myTmpl1(person);\n\n// Alternative syntax: var html = $.render.myTmpl1(person);\n\n// result: \"Name: Jim<br/> \"\n```\n\n[Learn more...](http://www.jsviews.com/#rendertmpl)\n\n### _Template tags_\n\n#### Template tag syntax\n\n- All tags other than `{{: ...}}` `{{> ...}}` `{{* ...}}` `{{!-- --}}` behave as *block tags*\n\n- Block tags can have content, unless they use the self-closing syntax:\n - *Block tag - with content:* `{{someTag ...}} content {{/someTag}}`\n - *Self-closing tag - no content (empty):* `{{someTag .../}}`\n\n- A particular case of self-closing syntax is when any block tag uses the named parameter `tmpl=...` to reference an external template, which then replaces what would have been the block content:\n \n - *Self-closing block tag referencing an external template:* `{{someTag ... tmpl=.../}}`\n (This lets you do [template composition](http://www.jsviews.com/#tagsyntax@composition). See [example](http://www.jsviews.com/#samples/jsr/composition/tmpl).)\n\n- Tags can take both unnamed arguments and named parameters:\n - `{{someTag argument1 param1=...}} content {{/someTag}}`\n - an example of a named parameter is the `tmpl=...` parameter mentioned above\n - arguments and named parameters can be assigned values from simple data-paths such as `address.street` or from richer expressions such as `product.quantity * 3.1 / 4.5`, or `name.toUpperCase()`\n\n[Learn more...](http://www.jsviews.com/#tagsyntax)\n\n#### Built-in tags\n\n#### _{{: ...}}_ (Evaluate)\n\n`{{: pathOrExpr}}` inserts the value of the path or expression.\n\n```js\nvar data = {address: {street: \"Main Street\"} };\nvar tmpl = $.templates(\"<b>Street:</b> {{:address.street}}\");\nvar html = tmpl.render(data);\n\n// result: \"<b>Street:</b> Main Street\"\n```\n\n[Learn more...](http://www.jsviews.com/#assigntag)\n\n#### _{{> ...}}_ (HTML-encode) \n\n`{{> pathOrExpr}}` inserts the *HTML-encoded* value of the path or expression.\n\n```js\nvar data = {condition: \"a < b\"};\nvar tmpl = $.templates(\"<b>Formula:</b> {{>condition}}\");\nvar html = tmpl.render(data);\n\n// result: \"<b>Formula:</b> a < b\"\n```\n\n[Learn more...](http://www.jsviews.com/#htmltag)\n\n#### _{{include ...}}_ (Template composition - partials)\n\n`{{include pathOrExpr}}...{{/include}}`evaluates the block content against a specified/modified data context.\n\n`{{include ... tmpl=.../}}` evaluates the specified template against an (optionally modified) context, and inserts the result. (Template composition).\n\n```js\nvar data = {name: \"Jim\", address: {street: \"Main Street\"} };\n\n// Register two named templates\n$.templates({\n streetTmpl: \"<i>{{:street}}</i>\",\n addressTmpl: \"{{:name}}'s address is {{include address tmpl='streetTmpl'/}}.\"\n});\n\n// Render outer template\nvar html = $.templates.addressTmpl(data);\n\n// result: \"Jim's address is <i>Main Street</i>\"\n```\n\n[Learn more...](http://www.jsviews.com/#includetag)\n\n#### _{{for ...}}_ (Template composition, with iteration over arrays)\n\n`{{for pathOrExpr}}...{{/for}}`evaluates the block content against a specified data context. If the new data context is an array, it iterates over the array, renders the block content with each data item as context, and concatenates the result.\n\n`{{for pathOrExpr tmpl=.../}}` evaluates the specified template against a data context. If the new data context is an array, it iterates over the array, renders the template with each data item as context, and concatenates the result.\n\n```html\n<script id=\"peopleTmpl\" type=\"text/x-jsrender\">\n <ul>{{for people}}\n <li>Name: {{:name}}</li>\n {{/for}}</ul>\n</script>\n```\n\n```js\nvar data = {people: [{name: \"Jim\"}, {name: \"Pedro\"}] };\nvar tmpl = $.templates(\"#peopleTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"<ul> <li>Name: Jim</li> <li>Name: Pedro</li> </ul>\"\n```\n\n[Learn more...](http://www.jsviews.com/#fortag)\n\n#### _{{props ...}}_ (Iteration over properties of an object)\n\n`{{props pathOrExpr}}...{{/prop}}` or `{{props pathOrExpr tmpl=.../}}` iterates over the properties of the object returned by the path or expression, and renders the content/template once for each property - using as data context: `{key: propertyName, prop: propertyValue}`.\n\n```html\n<script id=\"personTmpl\" type=\"text/x-jsrender\">\n <ul>{{props person}}\n <li>{{:key}}: {{:prop}}</li>\n {{/props}}</ul>\n</script>\n```\n\n```js\nvar data = {person: {first: \"Jim\", last: \"Varsov\"} };\nvar tmpl = $.templates(\"#personTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"<ul> <li>first: Jim</li> <li>last: Varsov</li> </ul>\"\n```\n\n[Learn more...](http://www.jsviews.com/#propstag)\n\n#### _{{if ...}}_ (Conditional inclusion)\n\n`{{if pathOrExpr}}...{{/if}}` or `{{if pathOrExpr tmpl=.../}}` renders the content/template only if the evaluated path or expression is 'truthy'.\n\n`{{if pathOrExpr}}...{{else pathOrExpr2}}...{{else}}...{{/if}}` behaves as '*if' - 'else if' - 'else'* and renders each block based on the conditions.\n\n```html\n<script id=\"personTmpl\" type=\"text/x-jsrender\">\n {{if nickname}}\n Nickname: {{:nickname}}\n {{else name}}\n Name: {{:name}}\n {{else}}\n No name provided\n {{/if}}\n</script>\n```\n\n```js\nvar data = {nickname: \"Jim\", name: \"James\"};\nvar tmpl = $.templates(\"#personTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"Nickname: Jim\"\n```\n\n[Learn more...](http://www.jsviews.com/#iftag)\n\n#### Other built-in tags\n\nFor details on all the above built-in tags, as well as *[comment tags](http://www.jsviews.com/#commenttag)* _{{!-- ... --}}_ and *[allow code tags](http://www.jsviews.com/#allowcodetag)* _{{\\* ... }} and {{\\*: ...}}_, see the [tags documentation](http://www.jsviews.com/#jsrtags) on jsviews.com.\n\n#### Custom tags\n\nCreating your own custom tags is easy. You can provide an object, with render method, template, event handlers, etc. See samples [here](http://www.jsviews.com/#samples/jsr/tags) and [here](http://www.jsviews.com/#samples/tag-controls) on jsviews.com. But for simple tags, you may only need a simple render function, or a template string. \n\nFor example the two following definitions for a `{{fullName/}}` tag provide equivalent behavior:\n\nAs a render function:\n\n```js\n$.views.tags(\"fullName\", function(val) {\n return val.first + \" \" + val.last;\n});\n```\n\nOr as a template string:\n\n```js\n$.views.tags(\"fullName\", \"{{:first}} {{:last}}\");\n```\n\nEither way, the result will be as follows:\n\n```js\nvar tmpl = $.templates(\"{{fullName person/}}\");\nvar data = {person: {first: \"Jim\", last: \"Varsov\"}};\nvar html = tmpl.render(data);\n\n// result: \"Jim Varsov\"\n```\n\n### _Helpers_\n\nFor details on helpers, see the [Helpers](http://www.jsviews.com/#helpers) documentation topic on jsviews.com.\n\nHere is a simple example. Two helpers - a function, and a string:\n\n```js\nvar myHelpers = {\n upper: function(val) { return val.toUpperCase(); },\n title: \"Sir\"\n};\n```\n\nAccess the helpers using the `~myhelper` syntax:\n\n```js\nvar tmpl = $.templates(\"{{:~title}} {{:first}} {{:~upper(last)}}\");\n```\n\nWe can pass the helpers in with the `render()` method\n\n```js\nvar data = {first: \"Jim\", last: \"Varsov\"};\n\nvar html = tmpl.render(data, myHelpers);\n\n// result: \"Sir Jim VARSOV\"\n```\n\nOr we can register helpers globally:\n\n```js\n$.views.helpers(myHelpers);\n\nvar data = {first: \"Jim\", last: \"Varsov\"};\nvar html = tmpl.render(data);\n\n// result: \"Sir Jim VARSOV\"\n```\n\n[Learn more...](http://www.jsviews.com/#helpers)\n\n### _Converters_\n\nConverters are used with the `{{:...}}` tag, using the syntax `{{mycvtr: ...}}}`.\n\nExample - an *upper* converter, to convert to upper case: \n\n```js\n$.views.converters(\"upper\", function(val) { return val.toUpperCase(); });\n\nvar tmpl = $.templates(\"{{:first}} {{upper:last}}\");\nvar data = {first: \"Jim\", last: \"Varsov\"};\nvar html = tmpl.render(data);\n\n// result: \"Jim VARSOV\"\n```\n\n[Learn more...](http://www.jsviews.com/#converters)\n\n### _Logic and expressions_\n\nJsRender supports rich expressions and logic, but at the same time encapsulates templates to prevent random access to globals. If you want to provide access to global variables within a template, you have to pass them in as data or as helpers.\n\nYou can assign rich expressions to any template arguments or parameters, as in:\n\n`{{:person.nickname ? \"Nickname: \" + person.nickname : \"(has no nickname)\"}}`\n\nor\n\n```html\n{{if ~limits.maxVal > (product.price*100 - discount)/rate}}\n ...\n{{else ~limits.minVal < product.price}}\n ... \n{{else}}\n ... \n{{/if}}\n```\n\n### _Documentation and APIs_\n\nSee the [www.jsviews.com](http://www.jsviews.com) site, including the *[JsRender Quickstart](http://www.jsviews.com/#jsr-quickstart)* and [JsRender APIs](http://www.jsviews.com/#jsrapi) topics.\n\n### _Demos_\n\nDemos and samples can be found at [www.jsviews.com/#samples](http://www.jsviews.com/#samples/jsr), and throughout the [API documentation](http://www.jsviews.com/#jsrapi).\n\n(See also the [demos](https://github.com/BorisMoore/jsrender/tree/master/demos) folder of the GitHub repository - available [here](http://borismoore.github.io/jsrender/demos/index.html) as live samples).\n", "dir"=>"/", "name"=>"README.md", "path"=>"README.md", "url"=>"/"} ");
We can pass the helpers in with the render()
method
var data = {first: "Jim", last: "Varsov"};
var html = tmpl.render(data, myHelpers);
// result: "Sir Jim VARSOV"
Or we can register helpers globally:
$.views.helpers(myHelpers);
var data = {first: "Jim", last: "Varsov"};
var html = tmpl.render(data);
// result: "Sir Jim VARSOV"
Converters are used with the `` tag, using the syntax }
.
Example - an upper converter, to convert to upper case:
$.views.converters("upper", function(val) { return val.toUpperCase(); });
var tmpl = $.templates("page{"permalink"=>"//", "layout"=>"home", "title"=>"JsRender: best-of-breed templating", "content"=>"## JsRender: best-of-breed templating\n\n[](https://www.npmjs.com/package/jsrender)\n[](https://cdnjs.com/libraries/jsrender)\n\n*Simple and intuitive, powerful and extensible, lightning fast*\n\n*For templated content in the browser or on Node.js (with Express 4, Hapi and Browserify integration)*\n\n**JsRender** is a light-weight but powerful templating engine, highly extensible, and optimized for high-performance rendering, without DOM dependency. It is designed for use in the browser or on Node.js, with or without jQuery.\n\n**[JsRender](https://github.com/BorisMoore/jsrender)** and **[JsViews](https://github.com/BorisMoore/jsviews)** together provide the next-generation implementation of the official jQuery plugins *[JQuery Templates](https://github.com/BorisMoore/jquery-tmpl)*, and *[JQuery Data Link](https://github.com/BorisMoore/jquery-datalink)* -- and supersede those libraries.\n\n### Documentation and downloads\n\n**[Documentation](http://www.jsviews.com)**, **[downloads](http://www.jsviews.com/#download)**, **[samples](http://www.jsviews.com/#samples)** and **[API docs and tutorials](http://www.jsviews.com/#jsrapi)** are available on the **[www.jsviews.com website](http://www.jsviews.com/#jsrender)**.\n\nThe content of this ***ReadMe*** is available also as a *[JsRender Quickstart](http://www.jsviews.com/#jsr-quickstart)*.\n\n### JsRender and JsViews\n\nJsRender is used for data-driven rendering of templates to strings, ready for insertion in the DOM.\n\nIt is also used by the *[JsViews](http://www.jsviews.com/#jsviews)* platform, which adds data binding to JsRender templates, and provides a fully-fledged MVVM platform for easily creating interactive data-driven single page apps and websites.\n\n## JsRender installation\n\n*jsrender.js* is available from [downloads](http://www.jsviews.com/#download) on the jsviews.com site. \n\n*CDN delivery* is available from the ***[jsdelivr](https://jsdelivr.com)*** CDN at [jsdelivr.com/package/npm/jsrender](https://jsdelivr.com/package/npm/jsrender?tab=files), and from the ***[cdnjs](https://cdnjs.com)*** CDN at [cdnjs.com/libraries/jsrender](https://cdnjs.com/libraries/jsrender).\nAlternatively:\n- It can be installed with **[Bower](http://bower.io/search/?q=jsrender)**, using `$ bower install jsrender` \n- It can be loaded using an *AMD script loader*, such as RequireJS\n- For installation using *Node.js* (*npm*) see *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)*\n- (For browser loading using *Browserify* or *webpack* - see *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)*, *[JsRender as a Browserify module](http://www.jsviews.com/#node/browserify@jsrender)* and *[JsRender as a webpack module](http://www.jsviews.com/#node/webpack@jsrender)*)\n\n#### Using JsRender with jQuery\n\nWhen jQuery is present, JsRender loads as a jQuery plugin and adds `$.views`, `$.templates` and `$.render` to the jQuery namespace object, `$` (or `window.jQuery`).\n\n*Example HTML page:* [JsRender with jQuery](http://www.jsviews.com/#download/pages-jsr-jq)\n\n#### Using JsRender without jQuery\n\nWhen jQuery is not present, JsRender provides its own global namespace object: `jsrender` (or `window.jsrender`)\n\nThe `jsrender` namespace provides the same methods/APIs as with jQuery, so if jQuery is not present you can still use all the API examples, by simply writing:\n\n```js\nvar $ = window.jsrender;\n\n// Now use code as in samples/examples, with $.views... $.templates... $.render...\n```\n(*Note:* If jQuery is not loaded, then [passing a jQuery selector](http://www.jsviews.com/#compiletmpl@fromscriptblock) to `$.templates()` will only work for the *ID selector*)\n\n*Example HTML page:* [JsRender without jQuery](http://www.jsviews.com/#download/pages-jsr)\n\n#### JsRender on Node.js\n\nJsRender can be used to render templates on the server (using Node.js) as well as in the browser. JsRender on Node.js has all the features and APIs of JsRender in the browser, plus some additional ones specific to Node.js.\n\nIt also provides built-in *Express*, *Hapi* and *Browserify* integration -- which makes it easy to register templates as simple `.html` files on the file system, and then load and render them either server-side, client-side or both.\n\n**Learn more:** *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)* and *[JsRender APIs for Node.js](http://www.jsviews.com/#jsrnode)*.\n\n**Code samples:** See *[JsRender Node Starter](https://github.com/BorisMoore/jsrender-node-starter)* for running code examples of Node.js scenarios, including with *Express*, *Hapi* and *Browserify*.\n\n## JsRender usage\n\n### _Define a template_\n\nFrom a string:\n\n```js\nvar tmpl = $.templates(\"Name: {{:name}}\");\n```\n\nFrom a template declared as markup in a script block:\n\n```html\n<script id=\"myTemplate\" type=\"text/x-jsrender\">\nName: {{:name}}\n</script>\n```\n\nthen, somewhere in your script:\n\n```js\nvar tmpl = $.templates(\"#myTemplate\"); // Pass in a jQuery selector for the script block\n```\n\nOn Node.js, [from an .html file](https://www.jsviews.com/#jsr-node-quickstart@htmlfiles) containing the template markup: \n\n```js\nvar $ = require('jsrender'); // returns the jsrender namespace object\nvar tmpl = $.templates(\"./templates/myTemplate.html\");\n```\n\n[Learn more...](http://www.jsviews.com/#d.templates)\n\n### _Render a template_\n\n`tmpl.render(object)` (or shortcut form: `tmpl(object)`) renders the template with the object as data context.\n\n```js\nvar tmpl = $.templates(\" Name: {{:name}}<br/> \");\n\nvar person = {name: \"Jim\"};\n\n// Render template for person object\nvar html = tmpl.render(person); // ready for insertion, e.g $(\"#result\").html(html);\n\n// result: \"Name: Jim<br/> \"\n```\n\n`tmpl.render(array)` (or `tmpl(array)`) renders the template once for each item in the array.\n\n```js\nvar people = [{name: \"Jim\"}, {name: \"Pedro\"}];\n\n// Render template for people array\nvar html = tmpl.render(people); // ready for insertion...\n\n// result: \"Name: Jim<br/> Name: Pedro<br/> \"\n```\n\n[Learn more...](http://www.jsviews.com/#rendertmpl)\n\n### _Register a named template - and render it_\n\n```js\n// Register named template - \"myTmpl1\n$.templates(\"myTmpl1\", \"Name: {{:name}}<br/> \");\n\nvar person = {name: \"Jim\"};\n\n// Render named template\nvar html = $.templates.myTmpl1(person);\n\n// Alternative syntax: var html = $.render.myTmpl1(person);\n\n// result: \"Name: Jim<br/> \"\n```\n\n[Learn more...](http://www.jsviews.com/#rendertmpl)\n\n### _Template tags_\n\n#### Template tag syntax\n\n- All tags other than `{{: ...}}` `{{> ...}}` `{{* ...}}` `{{!-- --}}` behave as *block tags*\n\n- Block tags can have content, unless they use the self-closing syntax:\n - *Block tag - with content:* `{{someTag ...}} content {{/someTag}}`\n - *Self-closing tag - no content (empty):* `{{someTag .../}}`\n\n- A particular case of self-closing syntax is when any block tag uses the named parameter `tmpl=...` to reference an external template, which then replaces what would have been the block content:\n \n - *Self-closing block tag referencing an external template:* `{{someTag ... tmpl=.../}}`\n (This lets you do [template composition](http://www.jsviews.com/#tagsyntax@composition). See [example](http://www.jsviews.com/#samples/jsr/composition/tmpl).)\n\n- Tags can take both unnamed arguments and named parameters:\n - `{{someTag argument1 param1=...}} content {{/someTag}}`\n - an example of a named parameter is the `tmpl=...` parameter mentioned above\n - arguments and named parameters can be assigned values from simple data-paths such as `address.street` or from richer expressions such as `product.quantity * 3.1 / 4.5`, or `name.toUpperCase()`\n\n[Learn more...](http://www.jsviews.com/#tagsyntax)\n\n#### Built-in tags\n\n#### _{{: ...}}_ (Evaluate)\n\n`{{: pathOrExpr}}` inserts the value of the path or expression.\n\n```js\nvar data = {address: {street: \"Main Street\"} };\nvar tmpl = $.templates(\"<b>Street:</b> {{:address.street}}\");\nvar html = tmpl.render(data);\n\n// result: \"<b>Street:</b> Main Street\"\n```\n\n[Learn more...](http://www.jsviews.com/#assigntag)\n\n#### _{{> ...}}_ (HTML-encode) \n\n`{{> pathOrExpr}}` inserts the *HTML-encoded* value of the path or expression.\n\n```js\nvar data = {condition: \"a < b\"};\nvar tmpl = $.templates(\"<b>Formula:</b> {{>condition}}\");\nvar html = tmpl.render(data);\n\n// result: \"<b>Formula:</b> a < b\"\n```\n\n[Learn more...](http://www.jsviews.com/#htmltag)\n\n#### _{{include ...}}_ (Template composition - partials)\n\n`{{include pathOrExpr}}...{{/include}}`evaluates the block content against a specified/modified data context.\n\n`{{include ... tmpl=.../}}` evaluates the specified template against an (optionally modified) context, and inserts the result. (Template composition).\n\n```js\nvar data = {name: \"Jim\", address: {street: \"Main Street\"} };\n\n// Register two named templates\n$.templates({\n streetTmpl: \"<i>{{:street}}</i>\",\n addressTmpl: \"{{:name}}'s address is {{include address tmpl='streetTmpl'/}}.\"\n});\n\n// Render outer template\nvar html = $.templates.addressTmpl(data);\n\n// result: \"Jim's address is <i>Main Street</i>\"\n```\n\n[Learn more...](http://www.jsviews.com/#includetag)\n\n#### _{{for ...}}_ (Template composition, with iteration over arrays)\n\n`{{for pathOrExpr}}...{{/for}}`evaluates the block content against a specified data context. If the new data context is an array, it iterates over the array, renders the block content with each data item as context, and concatenates the result.\n\n`{{for pathOrExpr tmpl=.../}}` evaluates the specified template against a data context. If the new data context is an array, it iterates over the array, renders the template with each data item as context, and concatenates the result.\n\n```html\n<script id=\"peopleTmpl\" type=\"text/x-jsrender\">\n <ul>{{for people}}\n <li>Name: {{:name}}</li>\n {{/for}}</ul>\n</script>\n```\n\n```js\nvar data = {people: [{name: \"Jim\"}, {name: \"Pedro\"}] };\nvar tmpl = $.templates(\"#peopleTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"<ul> <li>Name: Jim</li> <li>Name: Pedro</li> </ul>\"\n```\n\n[Learn more...](http://www.jsviews.com/#fortag)\n\n#### _{{props ...}}_ (Iteration over properties of an object)\n\n`{{props pathOrExpr}}...{{/prop}}` or `{{props pathOrExpr tmpl=.../}}` iterates over the properties of the object returned by the path or expression, and renders the content/template once for each property - using as data context: `{key: propertyName, prop: propertyValue}`.\n\n```html\n<script id=\"personTmpl\" type=\"text/x-jsrender\">\n <ul>{{props person}}\n <li>{{:key}}: {{:prop}}</li>\n {{/props}}</ul>\n</script>\n```\n\n```js\nvar data = {person: {first: \"Jim\", last: \"Varsov\"} };\nvar tmpl = $.templates(\"#personTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"<ul> <li>first: Jim</li> <li>last: Varsov</li> </ul>\"\n```\n\n[Learn more...](http://www.jsviews.com/#propstag)\n\n#### _{{if ...}}_ (Conditional inclusion)\n\n`{{if pathOrExpr}}...{{/if}}` or `{{if pathOrExpr tmpl=.../}}` renders the content/template only if the evaluated path or expression is 'truthy'.\n\n`{{if pathOrExpr}}...{{else pathOrExpr2}}...{{else}}...{{/if}}` behaves as '*if' - 'else if' - 'else'* and renders each block based on the conditions.\n\n```html\n<script id=\"personTmpl\" type=\"text/x-jsrender\">\n {{if nickname}}\n Nickname: {{:nickname}}\n {{else name}}\n Name: {{:name}}\n {{else}}\n No name provided\n {{/if}}\n</script>\n```\n\n```js\nvar data = {nickname: \"Jim\", name: \"James\"};\nvar tmpl = $.templates(\"#personTmpl\");\nvar html = tmpl.render(data);\n\n// result: \"Nickname: Jim\"\n```\n\n[Learn more...](http://www.jsviews.com/#iftag)\n\n#### Other built-in tags\n\nFor details on all the above built-in tags, as well as *[comment tags](http://www.jsviews.com/#commenttag)* _{{!-- ... --}}_ and *[allow code tags](http://www.jsviews.com/#allowcodetag)* _{{\\* ... }} and {{\\*: ...}}_, see the [tags documentation](http://www.jsviews.com/#jsrtags) on jsviews.com.\n\n#### Custom tags\n\nCreating your own custom tags is easy. You can provide an object, with render method, template, event handlers, etc. See samples [here](http://www.jsviews.com/#samples/jsr/tags) and [here](http://www.jsviews.com/#samples/tag-controls) on jsviews.com. But for simple tags, you may only need a simple render function, or a template string. \n\nFor example the two following definitions for a `{{fullName/}}` tag provide equivalent behavior:\n\nAs a render function:\n\n```js\n$.views.tags(\"fullName\", function(val) {\n return val.first + \" \" + val.last;\n});\n```\n\nOr as a template string:\n\n```js\n$.views.tags(\"fullName\", \"{{:first}} {{:last}}\");\n```\n\nEither way, the result will be as follows:\n\n```js\nvar tmpl = $.templates(\"{{fullName person/}}\");\nvar data = {person: {first: \"Jim\", last: \"Varsov\"}};\nvar html = tmpl.render(data);\n\n// result: \"Jim Varsov\"\n```\n\n### _Helpers_\n\nFor details on helpers, see the [Helpers](http://www.jsviews.com/#helpers) documentation topic on jsviews.com.\n\nHere is a simple example. Two helpers - a function, and a string:\n\n```js\nvar myHelpers = {\n upper: function(val) { return val.toUpperCase(); },\n title: \"Sir\"\n};\n```\n\nAccess the helpers using the `~myhelper` syntax:\n\n```js\nvar tmpl = $.templates(\"{{:~title}} {{:first}} {{:~upper(last)}}\");\n```\n\nWe can pass the helpers in with the `render()` method\n\n```js\nvar data = {first: \"Jim\", last: \"Varsov\"};\n\nvar html = tmpl.render(data, myHelpers);\n\n// result: \"Sir Jim VARSOV\"\n```\n\nOr we can register helpers globally:\n\n```js\n$.views.helpers(myHelpers);\n\nvar data = {first: \"Jim\", last: \"Varsov\"};\nvar html = tmpl.render(data);\n\n// result: \"Sir Jim VARSOV\"\n```\n\n[Learn more...](http://www.jsviews.com/#helpers)\n\n### _Converters_\n\nConverters are used with the `{{:...}}` tag, using the syntax `{{mycvtr: ...}}}`.\n\nExample - an *upper* converter, to convert to upper case: \n\n```js\n$.views.converters(\"upper\", function(val) { return val.toUpperCase(); });\n\nvar tmpl = $.templates(\"{{:first}} {{upper:last}}\");\nvar data = {first: \"Jim\", last: \"Varsov\"};\nvar html = tmpl.render(data);\n\n// result: \"Jim VARSOV\"\n```\n\n[Learn more...](http://www.jsviews.com/#converters)\n\n### _Logic and expressions_\n\nJsRender supports rich expressions and logic, but at the same time encapsulates templates to prevent random access to globals. If you want to provide access to global variables within a template, you have to pass them in as data or as helpers.\n\nYou can assign rich expressions to any template arguments or parameters, as in:\n\n`{{:person.nickname ? \"Nickname: \" + person.nickname : \"(has no nickname)\"}}`\n\nor\n\n```html\n{{if ~limits.maxVal > (product.price*100 - discount)/rate}}\n ...\n{{else ~limits.minVal < product.price}}\n ... \n{{else}}\n ... \n{{/if}}\n```\n\n### _Documentation and APIs_\n\nSee the [www.jsviews.com](http://www.jsviews.com) site, including the *[JsRender Quickstart](http://www.jsviews.com/#jsr-quickstart)* and [JsRender APIs](http://www.jsviews.com/#jsrapi) topics.\n\n### _Demos_\n\nDemos and samples can be found at [www.jsviews.com/#samples](http://www.jsviews.com/#samples/jsr), and throughout the [API documentation](http://www.jsviews.com/#jsrapi).\n\n(See also the [demos](https://github.com/BorisMoore/jsrender/tree/master/demos) folder of the GitHub repository - available [here](http://borismoore.github.io/jsrender/demos/index.html) as live samples).\n", "dir"=>"/", "name"=>"README.md", "path"=>"README.md", "url"=>"/"} ");
var data = {first: "Jim", last: "Varsov"};
var html = tmpl.render(data);
// result: "Jim VARSOV"
JsRender supports rich expressions and logic, but at the same time encapsulates templates to prevent random access to globals. If you want to provide access to global variables within a template, you have to pass them in as data or as helpers.
You can assign rich expressions to any template arguments or parameters, as in:
``
or
...
...
...
See the www.jsviews.com site, including the JsRender Quickstart and JsRender APIs topics.
Demos and samples can be found at www.jsviews.com/#samples, and throughout the API documentation.
(See also the demos folder of the GitHub repository - available here as live samples).