Skip to content

Instantly share code, notes, and snippets.

@tunnckoCore
Forked from jed/LICENSE.txt
Created June 4, 2014 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tunnckoCore/958871398cc8c1df2372 to your computer and use it in GitHub Desktop.
Save tunnckoCore/958871398cc8c1df2372 to your computer and use it in GitHub Desktop.
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.
)
}
)
}
}
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||{})})}}
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.
{
"name": "template",
"keywords": ["template", "HTML", "render"]
}
!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 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