If vowel add an else add a

Is there an easy way to check if the first letter in .Title is a vowel and then conditionally apply an instead of a?

Example"

  • An Apple
  • A Banana

This is what I’ve got so far:

{{$vowels := print "a"}}
{{cond (in (index (split (lower .Title) "") 0) $vowels) "an" "a" }}

This works, but if I add the other vowels and make an array, it stops working.

{{$vowels :=  slice "a" "e" "i" "o" "u"}}
{{cond (in (index (split (lower .Title) "") 0) $vowels) "an" "a" }}

I believe I need to range over $vowels to make this work, but I’m thinking there is a smarter way. I know there are singularize/pluralize functions. Is there something similar for this use case? How would you solve this?

I’ve played around with this some more. This is the working solution that I have right now:

{{if (substr .Title 0 1) | in (split "AEIOU" "")}} an {{else}} a {{end}}

The syntax for in is

in SET ITEM

The code you have above is the equivalent of
in $firstletter $vowels ==> in ITEM SET

Swapping around the two should work:

{{cond (in $vowels (index (split (lower .Title) "") 0 )) "an" "a" }}
1 Like