liquid icon indicating copy to clipboard operation
liquid copied to clipboard

add title case filter

Open dyspop opened this issue 8 years ago • 1 comments

A common problem with inputs is that they are entered oddly by users. While they are commonly backend filtered they are not always.

The Title Case filter capitalizes the first character only unless the input is all upper or lower, in which case use standard capitalization.

2.3.0 :002 > @template = Liquid::Template.parse("{{ name | title_case }}")
 => #<Liquid::Template: [...] > 
2.3.0 :003 > @template.render( 'name' => 'bob JONES mcAllister' )
 => "Bob Jones McAllister" 

This is essentially a much quicker version of:

{% capture input %}
  {% assign inputs = input | split: ' ' %}
  {% for input in inputs %}
    {% assign x = 0 %}
    {% assign y = 0 %}
    {% assign input_arr = input | split: '' %}
      {% for char in input_arr %}
        {% assign char_cap = char | capitalize %}
        {% if char_cap == char %}
          {% assign x = x | plus: 1 %}
        {% endif %}
        {% assign y = y | plus: 1 %}
      {% endfor %}
      {% if x == 0 or x == y %}
        {{ input | capitalize }} 
      {% else %}
        {% assign input_first_letter = input_arr[0] | capitalize %}
        {{ input_first_letter }}{{ input | replace_first: input_arr[0], '' }} 
      {% endif %}
  {% endfor %}
{% endcapture %}
{{ input | rstrip }}

I think this is a features that is likely to be useful to the majority of Liquid users.

dyspop avatar Mar 29 '17 13:03 dyspop

I know this is ancient.... Any intent to review?

dyspop avatar Dec 21 '18 20:12 dyspop