How to convert string to uppercase / lowercase in Jinja2?

Question:

I am trying to convert to upper case a string in a Jinja template I am working on.

In the template documentation, I read:

upper(s)
    Convert a value to uppercase.

So I wrote this code:

{% if student.department == "Academy" %}
    Academy
{% elif  upper(student.department) != "MATHS DEPARTMENT" %}
    Maths department
{% endif %}

But I am getting this error:

UndefinedError: 'upper' is undefined

So, how do you convert a string to uppercase in Jinja2?

Asked By: Xar

||

Answers:

Filters are used with the |filter syntax:

{% elif  student.department|upper != "MATHS DEPARTMENT" %}
    Maths department
{% endif %}

or you can use the str.upper() method:

{% elif  student.department.upper() != "MATHS DEPARTMENT" %}
    Maths department
{% endif %}

Jinja syntax is Python-like, not actual Python.

Answered By: Martijn Pieters

And you can use: Filter like this

{% filter upper %}
    UPPERCASE
{% endfilter %}
Answered By: saudi_Dev

for the capitalize

{{ 'helLo WOrlD'|capitalize }}

output

Hello world

for the uppercase

{{ 'helLo WOrlD'|upper }}

output

HELLO WORLD
Answered By: Jamil Noyda

For Capitalize

{{ 'helLo WOrlD'|capfirst }}

For UPPER CASE

{{ 'helLo WOrlD'|upper }}

For lower case

{{ 'helLo WOrlD'|lower }}

For title

{{ 'helLo WOrlD'|title }}

For ljust

{{ 'helLo WOrlD'|ljust }}

For rjust

{{ 'helLo WOrlD'|rjust }}

For wrap

{{ 'helLo WOrlD'|wrap }}

Hope It Helps

Answered By: Subhransu Das
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.