Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to uppercase / lowercase in Jinja2?

Tags:

python

jinja2

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?

like image 928
Xar Avatar asked Apr 21 '14 10:04

Xar


2 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.

like image 139
Martijn Pieters Avatar answered Oct 18 '22 19:10

Martijn Pieters


for the capitalize

{{ 'helLo WOrlD'|capitalize }}

output

Hello world

for the uppercase

{{ 'helLo WOrlD'|upper }}

output

HELLO WORLD
like image 39
Jamil Noyda Avatar answered Oct 18 '22 21:10

Jamil Noyda