Repository: trix/nano Branch: master Commit: a0a93b18879a Files: 4 Total size: 3.1 KB Directory structure: gitextract_s7ty_e4c/ ├── MIT-LICENSE ├── nano.js ├── readme.md └── testPage.html ================================================ FILE CONTENTS ================================================ ================================================ FILE: MIT-LICENSE ================================================ Copyright 2014 Tomasz Mazur, Jacek Becela Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: nano.js ================================================ /* Nano Templates - https://github.com/trix/nano */ function nano(template, data) { return template.replace(/\{([\w\.]*)\}/g, function(str, key) { var keys = key.split("."), v = data[keys.shift()]; for (var i = 0, l = keys.length; i < l; i++) v = v[keys[i]]; return (typeof v !== "undefined" && v !== null) ? v : ""; }); } ================================================ FILE: readme.md ================================================ NANO - Simplest Template Engine ============================= ***Basic Usage*** Assuming you have following JSON response:
data = {
user: {
login: "tomek",
first_name: "Thomas",
last_name: "Mazur",
account: {
status: "active",
expires_at: "2009-12-31"
}
}
}
you can make:
nano("<p>Hello {user.first_name} {user.last_name}! Your account is <strong>{user.account.status}</strong></p>", data)
and you get ready string:
<p>Hello Thomas Mazur! Your account is <strong>active</strong></p>
Test page: testPage.html
Simple huh?
***More Advanced Example***
Displaying list of twitter search results (JSONP API)
http://jsfiddle.net/UXZDy/86/
================================================
FILE: testPage.html
================================================
nano("<p>Hello {user.first_name} {user.last_name}! Your account is <strong>{user.account.status}</strong></p>", data)