Capitalize first letter of each word with liquid syntax?

Issue

I am trying to set up SEO in a LocomotiveCMS installation using liquid syntax. I’m trying to code it so that the page title is pulled dynamically using {{ page.title }} and then forcing it to capitalize the first letter of each word.

I tried this:

<title>
      {{ page.title | camelcase }} | {{ site.name }}
</title>

Based on the liquid syntax documentation here: http://docs.shopify.com/themes/liquid-basics/output#camelize

But it’s not working. Using capitalize works, but it only capitalizes the first letter of the first word.

Thanks!

Solution

I would suggest to use a plugin to obtain this behavior

_plugins/_capitalize_all.rb:

require 'liquid'
require 'uri'

# Capitalize all words of the input
module Jekyll
  module CapitalizeAll
    def capitalize_all(words)
      return words.split(' ').map(&:capitalize).join(' ')
    end
  end
end

Liquid::Template.register_filter(Jekyll::CapitalizeAll)

Usage:

{{ "mein text" | capitalize_all }}

Answered By – AsTeR

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.