-
-
Save jed/964762 to your computer and use it in GitHub Desktop.
write contextual templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
a, // the string source from which the template is compiled | |
b // the default `with` context of the template (optional) | |
){ | |
return function( | |
c, // the object called as `this` in the template | |
d // the `with` context of this template call (optional) | |
){ | |
return a.replace( | |
/#{([^}]*)}/g, // a regexp that finds the interpolated code: "#{<code>}" | |
function( | |
a, // not used, only positional | |
e // the code matched by the interpolation | |
){ | |
return Function( | |
"x", | |
"with(x)return " + e // the result of the interpolated code | |
).call( | |
c, // pass the data object as `this`, with | |
d // the most | |
|| b // specific | |
|| {} // context. | |
) | |
} | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||{})})}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "template", | |
"keywords": ["template", "HTML", "render"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function() { | |
// Put the template in your code... | |
var t = function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||{})})}} | |
// and make a template, using `#{<code>}` for JavaScript. | |
var hello = t("Hello, #{this.name || 'world'}!") | |
// Run the template with an object... | |
console.log( // => "Hello, Jed!" | |
hello({name: "Jed"}) | |
) | |
// or without. | |
console.log( // => "Hello, world!" | |
hello() | |
) | |
// Or make a template with a context... | |
var greet = t( | |
"Allow me to say, '#{hello(this)}'", | |
{hello: hello} | |
) | |
// and run it as is... | |
console.log( // => "Allow me to say, 'Hello, Jed!'" | |
greet({name: "Jed"}) | |
) | |
// or with its own context. | |
console.log( // => "Allow me to say, 'Hello, JED!'" | |
greet( | |
{name: "Jed"}, | |
{hello: function(x) { | |
return hello({ name: x.name.toUpperCase() }) | |
}} | |
) | |
) | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This test renders a table with every CJK ideograph. | |
// On my MacBook Air, it takes about 3.5 seconds. | |
!function() { | |
var t = function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||{})})}} | |
var kanji = [] | |
for (var i = 0x9faf; i >= 0x4e00; i--) { | |
kanji[i] = String.fromCharCode(i) | |
} | |
var d = new Date | |
var row = t( "\ | |
<tr>\ | |
<td>#{this}</td>\ | |
<td>\\u#{this.charCodeAt(0).toString(16)}</td>\ | |
</tr>" | |
) | |
var table = t( "\ | |
<h1>CJK unified ideographs</h1>\ | |
<table>\ | |
<tr>\ | |
<td><b>Glyph</b></td>\ | |
<td><b>Codepoint</b></td>\ | |
</tr>\ | |
#{this.map(row).join('')}\ | |
</table>", | |
{row:row} | |
) | |
console.log( "compiling templates..." ) | |
console.log( "compiled templates in " + (new Date - d) + "ms" ) | |
d = new Date | |
console.log( "rendering table..." ) | |
var html = table(kanji) | |
console.log( "rendered table in " + (new Date - d) + "ms" ) | |
console.log( "here's a sample:\n\n" + html.substr(0, 1000) ) | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Super)