if not 和 ct.viewless|default(false) in twig 是什么?
what is if not and ct.viewless|default(false) in twig?
我刚刚在 bolt CMS 中遇到了以下语法:
{% for ct in app.config.get('contenttypes') if not ct.viewless|default(false) %}
现在循环的开始似乎很熟悉 PHP 语法,但下面的部分出现在图片中:
if not ct.viewless|default(false)
现在那部分有点难理解,那部分代码到底在做什么??
或者实际上那部分代码真正在说什么??
P.S。上面的语法是 Twig 语法,它是一种 php 模板语言。
如果循环中元素的值没有 属性 viewless
或 [=21],则您发布的代码针对 contenttypes
的所有值进行循环=]为false则进入循环
Adding a condition
Unlike in PHP, it's not possible to break or continue in a loop. You
can however filter the sequence during iteration which allows you to
skip items. The following example skips all the users which are not
active:
<ul>
{% for user in users if user.active %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
The advantage is that the special loop variable will count correctly
thus not counting the users not iterated over. Keep in mind that
properties like loop.last will not be defined when using loop
conditions.
希望对您有所帮助
我刚刚在 bolt CMS 中遇到了以下语法:
{% for ct in app.config.get('contenttypes') if not ct.viewless|default(false) %}
现在循环的开始似乎很熟悉 PHP 语法,但下面的部分出现在图片中:
if not ct.viewless|default(false)
现在那部分有点难理解,那部分代码到底在做什么??
或者实际上那部分代码真正在说什么??
P.S。上面的语法是 Twig 语法,它是一种 php 模板语言。
如果循环中元素的值没有 属性 viewless
或 [=21],则您发布的代码针对 contenttypes
的所有值进行循环=]为false则进入循环
Adding a condition
Unlike in PHP, it's not possible to break or continue in a loop. You can however filter the sequence during iteration which allows you to skip items. The following example skips all the users which are not active:
<ul> {% for user in users if user.active %} <li>{{ user.username|e }}</li> {% endfor %} </ul>
The advantage is that the special loop variable will count correctly thus not counting the users not iterated over. Keep in mind that properties like loop.last will not be defined when using loop conditions.
希望对您有所帮助