Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created May 10, 2011 16:04
Show Gist options
  • Select an option

  • Save jed/964762 to your computer and use it in GitHub Desktop.

Select an option

Save jed/964762 to your computer and use it in GitHub Desktop.
write contextual templates
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 default `with` context of the template (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(
"b",
"with(b)return " + e // the result of the interpolated code
).call(
c, // the object passed to the template as `this`
d || b || c // the most specific `with` 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||c)})}}
Copyright (c) 2011 Jed Schmidt, http://jed.is
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "template",
"keywords": ["template", "HTML", "render"]
}
@caracal7
Copy link

caracal7 commented Mar 8, 2018

Super)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment