如何在Nunjucks中进行不区分大小写的字符串比较

编程入门 行业动态 更新时间:2024-10-21 17:25:07
本文介绍了如何在Nunjucks中进行不区分大小写的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 {%if%} 语句内进行不区分大小写的匹配

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

以下两种方法不起作用:

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

Nunucks的比较运算符,但未引用特定类型。

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

Nunjucks是Jinja2的端口,并且在如何在Jinja2中小写字符串有类似问题

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

推荐答案

您可以使用内置过滤器,例如 lower 来转换字符串或nunjucks允许您在表达式内部执行一组有限的JavaScript,因此调用 toLowerCase()也可以。

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:

1 {%if'app'== role.toLowerCase()%} 1 {%endif%} 2 {%如果'app'==角色|较低的%} 2 {%endif%} 3 {%set role_lower =‘App’|较低的%} {%,如果是 app == role_lower%} 3 {%endif%}

更多推荐

如何在Nunjucks中进行不区分大小写的字符串比较

本文发布于:2023-11-10 16:04:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小写   字符串   如何在   Nunjucks

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!