Skip to content

Instantly share code, notes, and snippets.

@stayradiated
Last active December 21, 2015 03:09
Show Gist options
  • Save stayradiated/6239816 to your computer and use it in GitHub Desktop.
Save stayradiated/6239816 to your computer and use it in GitHub Desktop.
# Super simple templating engine
tmpl = (template, namespace) ->
fn = (existing, fieldName) ->
content = namespace[fieldName]
content ?= existing
return content
template.replace(/\{\{([a-z0-9_]*)\}\}/ig, fn)
###
template = "Hello {{name}}!"
namespace = {
name: "world"
}
templ(template, namespace)
=> "Hello world!"
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment