The simplest jQuery template engine, with only nine lines of code, perfectly parses JSON.
/* Nano Templates (Tomasz Mazur, Jacek Becela) */(function ($) {$. nano = function (template, data) {return template. replace (/\ {([\ w \.] *) \}/g, function (str, key) {var keys = key. split (". "), value = data [keys. shift ()]; $. each (keys, function () {value = value [this] ;}); return (value = null | value = undefined )? "": Value;}) ;};} (jQuery );
Source Code address: https://github.com/trix/nano
Assume that you have the following JSON data:
Data = {user: {login: "tomek", first_name: "Thomas", last_name: "Mazur", account: {status: "active", expires_at: "2009-12-31 "}}}
You have the following template:
$. Nano ("<p> Hello {user. first_name} {user. last_name }! Your account is <strong> {user. account. status} </strong> </p> ", data)
You will get the following string:
<P> Hello Thomas! Your account is <strong> active </strong> </p>
Easy !!