Skip to content

@liquid /ba-ghettotmpl.js forked from cowboy/ba-ghettotmpl.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ghetto fabulous templating system. More ghetto than fabulous.
// Ghetto fabulous template system for replacing values in strings. If {{.foo}}
// or {{.bar[0].baz}} is encountered (leading . or ( or [ char), attempt to
// access properties of data object like `data.foo` or `data.bar[0].baz`.
// Alternately, if {{foo}} or {{bar("baz")}} is encountered (no leading dot),
// simply evaluate `foo` or `bar("baz")`. If an error occurs, return empty
// string. Oh yeah, you have to pass the result of ghettoTmpl to eval. :)
function ghettoTmpl(data, str) {
ghettoTmpl._data = data;
ghettoTmpl._str = str;
return "ghettoTmpl._str.replace(/\\{\\{(([.[(])?.*?)\\}\\}/g, function(_, str, dot) {"
+ "return eval('try{' + (dot ? 'ghettoTmpl._data' : '') + str + '}catch(e){\"\"}');"
+ "})";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.