Skip to content

Instantly share code, notes, and snippets.

@rjregenold
Created September 22, 2011 02:26
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 rjregenold/1233890 to your computer and use it in GitHub Desktop.
Save rjregenold/1233890 to your computer and use it in GitHub Desktop.
Simple Coffeescript templating
# inspired by this post:
# http://mir.aculo.us/2011/03/09/little-helpers-a-tweet-sized-javascript-templating-engine/
t = (s,d) ->
f = (p,c) -> p.replace new RegExp("{{#{c}}}", 'g'), d[c]
(Object.keys d).reduce f,s
# ---
# usage examples
console.log t("Hello, {{who}}! Do you like {{color}}?", who: 'John', color: 'green')
timeOfDay = ->
h = new Date().getHours()
return 'morning' if 5 <= h < 12
return 'afternoon' if 12 <= h < 19
return 'evening' if 19 <= h < 23
'night'
console.log t('Good {{timeOfDay}}!', {timeOfDay})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment