Created
September 22, 2011 02:26
-
-
Save rjregenold/1233890 to your computer and use it in GitHub Desktop.
Simple Coffeescript templating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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