4

Is it possible to convert a simple string ("Hello World!") to uppercase ("HELLO WORLD!")?

14

The upper filter is what you are looking for:

{% set var = 'Hello World' %}

{# Print in UPPERCASE #}
{{ var|upper }} {# 'HELLO WORLD' #}

Besides upper there are also some other filters available to modify the capitalization style of your strings. Of course there is the lowercase equivalent lower and additionally there are some Craft exclusive filters available.

<ul>
  {# Set test strings #}
  {% set strLower = 'this is a test string' %}
  {% set strUpper = 'THIS IS A TEST STRING' %}
  {% set strMixed = 'this Is a TEST stRing' %}

  {# Apply Twig filters #}
  <li>upper: {{ strLower|upper }}</li> {# 'THIS IS A TEST STRING' #}
  <li>lower: {{ strUpper|lower }}</li> {# 'this is a test string' #}

  {# Apply filters provided by Craft #}
  <li>ucwords: {{ strLower|ucwords }}</li> {# 'This Is A Test String' #}
  <li>ucfirst: {{ strLower|ucfirst }}</li> {# 'This is a test string' #}
  <li>lcfirst: {{ strUpper|lcfirst }}</li> {# 'tHIS IS A TEST STRING' #}      

  {# Combine filters for predictable application #}
  <li>ucwords: {{ strMixed|lower|ucwords }}</li> {# 'This Is A Test String' #}
  <li>ucfirst: {{ strMixed|lower|ucfirst }}</li> {# 'This is a test string' #}
  <li>lcfirst: {{ strMixed|upper|lcfirst }}</li> {# 'tHIS IS A TEST STRING' #}

  {# Examples for possible use cases #}
  <li>PascalCase: {{ strMixed|lower|ucwords|replace(' ', '') }}</li> {# 'ThisIsATestString' #}
  <li>camelCase: {{ strMixed|lower|ucwords|lcfirst|replace(' ', '') }}</li> {# 'thisIsATestString' #}
</ul>
  • Sorry Victor, actually I wanted to make this edit only a proposal. Don't know why it applied directly!? SE is soooo new to me! You can revert this and I add an additional answer, if you want. – carlcs Jun 14 '14 at 13:52
  • Over 500reps SE allows you to edit directly, and why should somebody have something against it? ;) – Victor In Jun 14 '14 at 13:55
  • So there no way to just suggest changes if you have this "privilege"? Don't like that actually. – carlcs Jun 14 '14 at 13:57
  • I don't think so, but I'm also pretty new to this. – Victor In Jun 14 '14 at 13:58
  • 1
    @ChristianSeelbach No, there is no way. Also, you now can review the edits, suggested by "small" users. You earn this privilege at 500 reputation points in private betas, at 1,000 reputation points in public betas and at 2,000 reputation points in completely launched sites. – nicael Jun 14 '14 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.