Display JSON Data Using the JsRender Template Engine | HTML Goodies

Display JSON Data Using the JsRender Template Engine

Apr 14, 2017
4 minute read

Display JSON Data Using the JsRender Template Engine

In my recent article on JavaScript Template Engines, we learned how Template Engines help us separate HTML from JavaScript, thereby reducing the outputting of HTML within JavaScript strings. Having observed their benefits and how to work with them, the time has come to try a light-weight but powerful templating engine named JsRender. In this tutorial, we’ll insert a JSON array of names into our HTML document using only three lines of JS code.

A Little Background

JsRender has an interesting history because it managed by the JQuery team and supersedes their original “JQuery Templates” plugin. Although the original project’s GitHub page is still there, it has not been updated since vBeta1.0.0 back in 2011. JsRender’s codeless syntax provides better separation of presentation and behavior, and includes some other new features and improvements that provide considerable advantages, such as the ease of creating custom tags, and increased performance. It also reduces the amount of JavaScript code significantly.

If you visit the JsRender homepage, you’ll notice that the page is divided into three related projects: JsRender, JsViews, and JsObservable. Although we’ll be concentrating on JsRender here today, let’s take a moment to explorer what each framework does.

JsRender is the templating engine. JsRender helps you output HTML using a template, that is a static HTML/CSS fragment with embedded tokens that get replaced with data. It supports simple logic, rendering values, and custom functions.

JsViews is the data binding engine. Built on top of JsRender, JsViews adds observability to objects and properties. This allows you to link your json objects to HTML elements via two-way data binding.

The JsObservable library is part of JsViews. It is used by JsViews to provide the declarative data-binding. It also allows your code in a JsViews app to trigger data changes, or to “observe” (i.e. listen for) data-changes programmatically.

Advertisement

JsRender Installation

JsRender is available for download on the jsviews.com site. Alternatively, you can reference one of the jsrender.js scripts hosted on the cdnjs servers:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jsrender/0.9.84/jsrender.min.js"></script>

Using JsRender without jQuery

Interestingly enough, even though JsRender replaced jQuery Templates, jQuery is not a requirement for running JsRender. That’s because, when jQuery is not present, JsRender provides its own jsrender namespace object, exposed as window.jsrender. The jsrender namespace exposes all of the same methods/APIs as jQuery, so you can still access API methods by assigning the jsrender object to the dollar sign ($):

var $ = window.jsrender;

//or, if you really want to make sure that $ is global
$ = window.jsrender;

//or even
window.$ = window.jsrender;

That will make member methods like $.views, $.templates, $.render available.

The Data

As mentioned in the intro, our data consists of an array of names. Each name object contains three attributes: the name, nickname, and whether or not to display the nickname:

var names = [
  {
    "name": "Robert",
    "nickname": ["Blackjacques", "Rob"],
    "showNickname": true
  },
  {
    "name": "Jeanie",
    "nickname": "",
    "showNickname": false
  }
];

Defining a Template Block

You can use client-side templates by referencing the JsRender script mentioned above and then defining a

Robert Gravelle

Rob Gravelle resides in Ottawa, Canada, and has been an IT guru for over 20 years. In that time, Rob has built systems for intelligence-related organizations such as Canada Border Services and various commercial businesses. In his spare time, Rob has become an accomplished music artist with several CDs and digital releases to his credit.

HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.