Skip to content

Instantly share code, notes, and snippets.

@mikeknoop
Created March 18, 2013 07:46
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 mikeknoop/5185647 to your computer and use it in GitHub Desktop.
Save mikeknoop/5185647 to your computer and use it in GitHub Desktop.
# accessor: returns the article "a" or "an" depending on the context string. Eg. "Computer" will return "a"
# and "Elephant" will return "an". Can pass a hash option `capitalize` which will capitalize the article.
# usage: {{#article}}string{{/article}} outputs "a string"
Handlebars.registerHelper "article", (options) ->
# options: pass `capitalize` true to get a capital article
string = options.fn(@)
vowels = ['a', 'e', 'i', 'o', 'u']
vowel = false
letter = string.toLowerCase()[0]
vowel = true for v in vowels when letter == v
article = 'an ' if vowel
article = 'a ' if not vowel
article = _.str.capitalize(article) if options.hash?.capitalize
return article + string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment