The one thing that sucks about projects with one-char names is finding them weeks after you first discover them, when you really want to experiment with using them in a real project.
Browser bookmarks are a way unrated feature. A descriptive title and you're good to go. Who cares what the name is when your bookmark title has "t.js A tiny javascript templating framework".
I already can't remember the name of half the libraries I see even when they are clever and appropriate.
While true, 'data' should always just be an object-literal. If template() is ever passed an instance of an object, there's something else seriously wrong elsewhere in your script.
Edit: On second thought, I think the previous gist can reach parity with just a helper function for iterating and escaping:
function engine(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||{})})}}
function iterator(o, k, f){
var i, v = [];
for(i in o[k]){
v = v.concat(f(o, k, i));
}
return v.join('');
}
function print_kv(o, k, i){
return [" ", i, ":", o[k][i]];
}
hello = engine("Hello,#{iterator(this, 'users', print_user)}!")
// We don't have to pass iterator, on the next line, but it should demonstrate
// how we context can be overwritten
hello({users: {a: 1, b: 2, c:3}}, {iterator: iterator, print_user: print_kv})
// Or don't pass print_user
print_user = print_kv
hello({users: {a: 1, b: 2, c:3}})
// "Hello, a:1 b:2 c:3!"
// Overload it!
hello({users: {a: 1, b: 2, c:3}}, {print_user: function(o, k, i){return " "+i}})
// "Hello, a b c!"
I had to zoom in like 3 times on my phone to vote this up. I know it may be bad form and all to comment on such, but guhdamn if as newb this doesn't wrinkle my britches.
Out of curiosity, do you get to directly place code on the L2 cache through the Lua VM ? Is that a module ? If so, what is this voodoo and where can I find it...
You don't need to store the framework you are sending to clients in the CPU cache - a good network card can read the data directly from disk, or main memory and send it off, bypassing the cpu.
No, this thing isn't a software platform. You cannot build any applications with this alone. This is a library. We have these precise terms. Don't abuse them.
I am also tired of libraries being called frameworks. I wouldn't even call this a library. A library infers a collection of something. This is just a simple single function.
Stating his opinion is not being pedantic, quoting the definition to make a point is being pedantic. I think what he's saying has merit; I would call this a utility, or something more along those lines, rather than a framework.
Thank you... and sorry for the rant but -- the word "pedantic" is incredibly overused of late on this site.
According to some quick Google site: searches:
WORD USE ON HACKER NEWS
pedantic 3340
thoughtful 3750
f*** 9930
idiot 6180
Which seems quite frequent, but is obviously highly anecdotal. On the other hand, it appearing 1/3 as much as f* may indicate that this is at least a fairly civil community -- even if prone to pedantry.
function render(tpl,data){
var matches = tpl.match(/{[^\}]+}/g);
for(var i in matches){
var rep = eval("data."+matches[i].replace('{','').replace('}',''));
tpl = tpl.replace(matches[i],rep);
}
return tpl;
}
alert(render("{a} is all that i've {b.a} {b.b} {b.c}", {a:"this", b:{a:"ever",b:"really",c:"needed"} }))
the code i use in production doesn't use eval, but a recursive function.
i like the anonymous function passed to replace, but think the catch destroys code beauty :)
render("{a} is all that I've {a}", {'a' : 'b'});
"b is all that I've b"
render("{a} is all that I've {{a}}", {'a' : 'b'});
SyntaxError: Unexpected token {
function render(tpl,data){
var matches = tpl.match(/{[^\}]+}/g);
for(var i in matches){
var rep = eval("data."+matches[i].replace(/[{}]/g,''));
tpl = tpl.replace(matches[i],rep);
}
return tpl;
}
The main advantage that I see at first glance is that this library, unlike lots of others (including those two), doesn't use eval or "new Function" to build compiled templates, which meets it meets Chrome's new security requirements for extensions.