Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter of each word with liquid syntax?

Tags:

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!

like image 733
APAD1 Avatar asked Jan 23 '14 17:01

APAD1


People also ask

What is it called when you capitalize the first letter of every word?

Title case, which capitalizes the first letter of certain key words. Sentence case, in which titles are capitalized like sentences. Initial case, where the first letter of every word is capitalized.

How do you capitalize the beginning of each word?

To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.

Which function capitalize the first letter in each word of a text value?

var str = text. toLowerCase().


2 Answers

There is a way to achieve this using only Liquid syntax. No need for any plugins.

Break down your string of words into an array and use a for loop combined with the capitalize filter to capitalize the first letter of each word. If you appropriately encapsulate this inside a capture statement you are left with the first character in every word capitalized.

{% assign words = "Hi, how are you today?" | split: ' ' %}  {% capture titlecase %}   {% for word in words %}     {{ word | capitalize }}   {% endfor %}{% endcapture %} {{ titlecase }} 

Output:

Hi, How Are You Today?

Notice that all of this is on a single line and there is only one occurrence of whitespace within the entire capture statement!

like image 154
taky2 Avatar answered Oct 15 '22 19:10

taky2


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 }} 
like image 38
AsTeR Avatar answered Oct 15 '22 19:10

AsTeR