Skip to content

Instantly share code, notes, and snippets.

@stewartknapman
Last active August 12, 2018 21:27
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 stewartknapman/92c2bc0ffa1ded2c742140b1fd0c1088 to your computer and use it in GitHub Desktop.
Save stewartknapman/92c2bc0ffa1ded2c742140b1fd0c1088 to your computer and use it in GitHub Desktop.
{% comment %}
Title case a string of text.
Usage: {% include 'title-case' string: 'my string' %}
{% endcomment %}
{%- capture str -%}{%- endcapture -%}
{%- assign small_words = 'a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs|vs.|via|with' | split:'|' -%}
{%- assign string_parts = string | split: ' ' -%}
{%- for p in string_parts -%}
{%- assign is_small = false -%}
{%- for s in small_words -%}
{% if p == s %}{%- assign is_small = true -%}{% endif %}
{%- endfor -%}
{%- capture str %}{% unless forloop.first %}{{ str }} {% endunless %}{% if is_small %}{{ p }}{% else %}{{ p | capitalize }}{% endif %}{% endcapture -%}
{%- endfor -%}{{- str -}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment