Remove hyphens and replace with spaces and capitalize in liquid

liquid, regex, shopify

Solution

You can split the tag on '-' then loop over the words capitalising each one:

  {% assign words = current_tags.first | split: '-' %}
  {% for word in words %}{{ word | capitalize }} {% endfor %}  

Problem

Using liquid, I am using product tags to generate page titles. I want to the page titles to be free from hyphens and each word capitalized. EXAMPLE: If the tag (used to generate the title) is "potato-chips". I want the title to be "Potato Chips". If I use: ``` {{current_tags.first}} ``` It generates a title of "potato-chips". If I use: ``` {{current_tags.first | replace: '-', ' '}} ``` It generates a title of "potato chips". If I use ``` {{current_tags.first | replace: '-', ' ' | capitalize}} ``` we get a title of "Potato chips". If any one knows how I can get the title "Potato Chips", that would be great. If it matters, some tags (used to generate the titles) are "word"; some are "word-word"; some are "word-word-word"; and so on. Thanks.

Original source