Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Titleize Jekyll category

Tags:

jekyll

liquid

I'd like to convert the printed category names of my posts into title case. I couldn't find a Liquid filter that would work. I tried using dashes and the camelcase filter, but no dice.

Alternatively, I'd like to print the category name as it's written in the YAML frontmatter.

For instance, for a post with:

category: Here's the Category

When I reference the name:

{% for cat in site.categories %}
    <h1>{{ cat[0] }}</h1>
{% endfor %}

I see "here's the category" on the page. I would like to see "Here's the Category" or even "Here's The Category," and I could replace (replace: 'The', 'the') the few articles that I wanted to be downcase.

EDIT

For anyone as desperate as I am, this disgusting hack works, where n is the max number of words you have in a category title.

{% for cat in site.categories %}
    {% assign words = cat[0] | split: ' ' %}
    <h1>{{ words[0] | capitalize | replace:'The','the'}} {{ words[1] | capitalize }} {{ words[2] }} {{ words[3] | capitalize }} {{ words[4] | capitalize }} {{ words[n] | capitalize }}</h1>
{% endfor %}

I'm going to leave the question unanswered in case someone knows a more elegant method.

like image 818
gwezerek Avatar asked Oct 29 '13 03:10

gwezerek


1 Answers

You can achieve a part of what you want by using the capitalize filter:

Input

{{ 'capitalize me' | capitalize }}

Output

Capitalize me

Source.

Another possibility, which I didn't test for its edge cases, is to use join and camelize:

{{ "here's the category" | join: '-' | camelize }}

It should print "Here's The Category", but camelize might have a problem with here's.

like image 127
agarie Avatar answered Sep 21 '22 12:09

agarie