trix / nano Goto Github PK
View Code? Open in Web Editor NEWSimplest Templating Engine
Home Page: http://trix.pl
License: MIT License
Simplest Templating Engine
Home Page: http://trix.pl
License: MIT License
Just looked at nano.js and you could easily make it several characters. It is interesting that you spend time making should you do not output null or undefined, but don't check that undefined[keys[]] or null[keys[]] is not protected, you could put it in the while(i < l && ...) adds code though.
function nano(template, data) {
return template.replace(/{([\w.]*)}/g, function(str, key) {
var keys = key.split("."), v = data[keys.shift()], i = 0, l = keys.length;
while(i < l) v = v[keys[i++]];
//simple while, hoisted variables up to avoid extra 'var', increment on use
return (typeof v == "undefined" || v == null) ? '' : v;
//change double negative save two chars.
});
}
Just sharing what I happen to see, really not any issue and I can close out either way.
thanks for this great piece of code, my only issue was that it does not work if the key has a space in it.
for example
{
"sku": "sdf3de",
"product name": "widget2"
}
it will work with sku, but not with "product name"
var template = "
$.getJSON("http://search.twitter.com/search.json?callback=?", {
q: query
}, function(data) {
container.html("")
$.each(data.results), function(i, item){
container.append($.nano(template, item))
}
}
this is never work becouse wrong position of ')' after data.results
in $.each(data.results),
Way to escape. Current work around I found is to write it as "{output{}}" to display "{output}" need a cleaner way such as {{output}}
fiddle's version work, while github's version don't.
please check the code. thank you.
btw, this code snippet is very Q and powerful. good job!
code:
define('nano', [], function(){
return function (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 : "";
});
};
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.