Re:T.js - A tiny templating framework in ~400 bytes
by whalesalad on Monday August 13, 2012 @07:36PM
(#4378696)
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.
Re:T.js - A tiny templating framework in ~400 bytes
by mcpherrinm on Monday August 13, 2012 @08:30PM
(#4378838)
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.
Re:T.js - A tiny templating framework in ~400 bytes
by drgath on Tuesday August 14, 2012 @08:17AM
(#4380490)
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!"
Re:T.js - A tiny templating framework in ~400 bytes
by obituary_latte on Monday August 13, 2012 @11:19PM
(#4379222)
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.
Re:T.js - A tiny templating framework in ~400 bytes
by novalis on Monday August 13, 2012 @09:53PM
(#4379011)
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...
Re:T.js - A tiny templating framework in ~400 bytes
by ars on Monday August 13, 2012 @11:52PM
(#4379304)
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.
Re:T.js - A tiny templating framework in ~400 bytes
by riobard on Monday August 13, 2012 @11:18PM
(#4379219)
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.
Re:T.js - A tiny templating framework in ~400 bytes
by steve19 on Tuesday August 14, 2012 @12:54AM
(#4379473)
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.
Re:T.js - A tiny templating framework in ~400 bytes
by shock3naw on Monday August 13, 2012 @10:41PM
(#4379132)
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.
Re:T.js - A tiny templating framework in ~400 bytes
by sakai on Monday August 13, 2012 @10:49PM
(#4379157)
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.
Re:T.js - A tiny templating framework in ~400 bytes
by vain on Monday August 13, 2012 @07:36PM
(#4378698)
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"} }))
Re:T.js - A tiny templating framework in ~400 bytes
by vain on Monday August 13, 2012 @08:04PM
(#4378775)
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 :)
Re:T.js - A tiny templating framework in ~400 bytes
by alexgartrell on Monday August 13, 2012 @07:41PM
(#4378710)
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 {
Re:T.js - A tiny templating framework in ~400 bytes
by pfraze on Monday August 13, 2012 @07:53PM
(#4378740)
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;
}
Re:T.js - A tiny templating framework in ~400 bytes
by apendleton on Tuesday August 14, 2012 @02:20AM
(#4379656)
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.
I already can't remember the name of half the libraries I see even when they are clever and appropriate.
Or Catch.com or pinboard.in or similar.
I also tend to print Web pages to PDF and stash them in a Dropbox folder for later review.
Or, there is now, anyway.
* Looks coincidentally similar to Fuchs'
Notably, the examples found at https://gist.github.com/964762#file_test.js has the closest parity.
Edit: On second thought, I think the previous gist can reach parity with just a helper function for iterating and escaping:
EDIT Xeon 3050 has 2MB of L2 cache, so the answer is probably yes!
I haven't properly updated the gist fork yet, but as follows are the semi-minified additions [of `safe` and `map` (iter)] and parity tests.
http://en.wikipedia.org/wiki/Software_framework
There's nothing in there about how lightweight they have to be to not be considered a framework, so I think you're being a tad pedantic.
That would make him wrong, not pedantic.
According to some quick Google site: searches:
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.Edit: formatting.
More micro-frameworks at http://microjs.com
return s(val).replace(re, function(t){
});