4:54a.m. *Mephisto-Fu — Nice Tags
I’m silly. I like my tags to read:
tag-one, tag-two and tag-three
Instead of:
tag-one, tag-two, tag-three
When I used WordPress, I used a plugin called Nice Categories to have an ‘and’ added and comma removed before the last tag. Thanks to Liquid, I can do the same without a plugin. In your default article template, it would be:
{% for tag in article.tags %}
{% if forloop.length == 1 %}
{{ tag | link_to_tag }}
{% elsif forloop.length == 2 %}
{% if forloop.last %}
and {{ tag | link_to_tag }}
{% else %}
{{ tag | link_to_tag }}
{% endif %}
{% elsif forloop.length > 2 %}
{% if forloop.last %}
and {{ tag | link_to_tag }}
{% elsif forloop.rindex == 2 %}
{{ tag | link_to_tag }}
{% else %}
{{ tag | link_to_tag }},
{% endif %}
{% endif %}
{% endfor %}
Yay! If you have any questions, feel free to ask. :)
Comments!
I love your site! I must be missing something on this post. What is the difference between the two versions of: tag_one... and tag_one... thx, Jamey
Thanks! The only difference is better looking grammar. It adds an 'and' and removes a comma from the last tag.
That could probably be cleaned up. Rails adds a generic #to_sentence method for arrays.
Comments are closed for this article.