jsrender

JsRender: best-of-breed templating

CDNJS version

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 and downloads

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 and JsViews

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 installation

jsrender.js is available from downloads on the jsviews.com site.

CDN delivery is available from the cdnjs CDN at cdnjs.com/libraries/jsrender.

Alternatively:

Using JsRender with jQuery

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

Using JsRender without 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 on Node.js

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.

JsRender usage

Define a template

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");

Learn more…

Render a template

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/> "

Learn more…

Register a named template - and render it

// 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/> "

Learn more…

Template tags

Template tag syntax

Learn more…

Built-in tags

__ (Evaluate)

`` 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"

Learn more…

__ (HTML-encode)

`` 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 &lt; b"

Learn more…

__ (Template composition - partials)

...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>"

Learn more…

__ (Template composition, with iteration over arrays)

...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>"

Learn more…

__ (Iteration over properties of an object)

... 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>"

Learn more…

__ (Conditional inclusion)

... 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"

Learn more…

Other built-in tags

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.

Custom tags

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", "content ");

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"

Helpers

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(" content ");

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"

Learn more…

Converters

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("content ");
var data = {first: "Jim", last: "Varsov"};
var html = tmpl.render(data);

// result: "Jim VARSOV"

Learn more…

Logic and expressions

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


  ...

  ... 

  ... 

Documentation and APIs

See the www.jsviews.com site, including the JsRender Quickstart and JsRender APIs topics.

Demos

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).