How to do case insensitive string comparison in Nunjucks

送分小仙女□ 提交于 2020-01-16 08:46:08

问题


I'm trying to do a case insensitive match inside of an {% if %} statement

The following two approaches do not work:

{% set role = 'APP' %}

{% if 'app' == role %}  1 {% endif %}
{% if 'app' in role  %} 2 {% endif %}

Nunucks only has a little documentation on their comparison operators, but don't refer to specific types.

Nunjucks is a port of Jinja2 and there is a similar question on how to lowercase a string in Jinja2


回答1:


You can use one of the built in filters like lower to transform the string or nunjucks allows you to execute a limited set of JavaScript inside of expressions so calling toLowerCase() will also work.

Any of the following 3 approaches will work:

{% set role = 'APP' %}

{% if 'app' == role.toLowerCase() %} 1 {% endif %}
{% if 'app' == role | lower %}       2 {% endif %}

{% set role_lower = 'App' | lower %}
{% if 'app' == role_lower %}         3 {% endif %}


来源:https://stackoverflow.com/questions/59253725/how-to-do-case-insensitive-string-comparison-in-nunjucks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!