你想要的图案是sm sm lg
打印正常,然后逆转,然后正常...
sm sm lg lg sm sm sm sm lg ...
您可以创建一个macro
(一function in Twig)输出这种模式:
{% macro printPattern(reverse = false) %} {# the function AKA macro signature #}
{% set sm = 'a small image' %} {# Make this an <img> tag. #}
{% set lg = 'a large image' %}
{% set pattern = (reverse) ? [lg, sm, sm] : [sm, sm, lg] %}
{% for n in pattern %}{{ n }} {% endfor %}
{% endmacro %}
{# If you wanted to use the macro in the same file you must include: #}
{% import from _self as helper %}
{# Then call the macro: #}
{{ helper.printPattern(false) }}
{{ helper.printPattern(true) }}
{{ helper.printPattern() }} {# This does the same as helper.printPattern(false) #}
{# because we set a default value for the passed-in variable in the macro's signature. #}
Example
特别说明:
我使用的操作者ternary
?
在这条线:
{% set pattern = (reverse) ? [lg, sm, sm] : [sm, sm, lg] %}
(回想reverse
是boolean
值,即true
或false
)。
该行等同于:
{% if reverse %}
{% set pattern = [sm, sm, lg] %}
{% else %}
{% set pattern = [lg, sm, sm] %}
{% endif %}
的Ternary operator是对于一些有条件的数据赋值给一个变量非常方便。
我不确定你想在这里捕捉什么样的图案。 – JETM
是的,请明确说明您可能需要什么! –
我基本上想要在前两次触发“真”,然后“假”两次,然后“真”四次,如此类推(如上图)。 –