Twig 我们可以在细枝循环中切换值吗
当包含一个细枝文件,比如说显示一个两列布局,一边有一个图像,另一边有一个copytext时,设计者通常要求在每一行中切换图像位置。第一行的图像在左边,第二行的图像在右边,依此类推。 我通常通过向上计数值Twig 我们可以在细枝循环中切换值吗,twig,Twig,当包含一个细枝文件,比如说显示一个两列布局,一边有一个图像,另一边有一个copytext时,设计者通常要求在每一行中切换图像位置。第一行的图像在左边,第二行的图像在右边,依此类推。 我通常通过向上计数值image\u orientation\u count并根据其是否为奇数,向包含的细枝文件传递一些标志,如下所示: {% set image_orientation_count = 1 %} {% for mw_id in post.modeteaser_list %} {% set tease
image\u orientation\u count
并根据其是否为奇数,向包含的细枝文件传递一些标志,如下所示:
{% set image_orientation_count = 1 %}
{% for mw_id in post.modeteaser_list %}
{% set teaser = TimberPost(mw_id) %}
{% if image_orientation_count is odd %}
{% set image_orientation = 'left' %}
{% else %}
{% set image_orientation = 'right' %}
{% endif %}
{% include 'templates/blocks/section-image-wide-text-small.twig' with {'title': teaser.title, 'copytext': teaser.copytext, 'mood_image': teaser.mood_image, 'button_url': teaser.button_url, 'button_label': teaser.button_label, 'image_orientation': image_orientation } only %}
{% set image_orientation_count = image_orientation_count + 1 %}
{% endfor %}
{% for ... %}
{% include "foo.twig" with {
"image_orientation": cycle(["left", "right"], loop.index0)
} only %}
{% endfor %}
我想知道是否有更优雅的方法来解决这类问题。不知何故,我用的方式感觉“便宜”;) 我建议你:
{% for mw_id in post.modeteaser_list %}
{% set teaser = TimberPost(mw_id) %}
{% set image_orientation = loop.index is odd ? 'left' : 'right' %}
{% include 'templates/blocks/section-image-wide-text-small.twig' with {
'title': teaser.title,
'copytext': teaser.copytext,
'mood_image': teaser.mood_image,
'button_url': teaser.button_url,
'button_label': teaser.button_label,
'image_orientation': image_orientation
} only %}
{% endfor %}
使用and三元条件运算符,我建议您:
{% for mw_id in post.modeteaser_list %}
{% set teaser = TimberPost(mw_id) %}
{% set image_orientation = loop.index is odd ? 'left' : 'right' %}
{% include 'templates/blocks/section-image-wide-text-small.twig' with {
'title': teaser.title,
'copytext': teaser.copytext,
'mood_image': teaser.mood_image,
'button_url': teaser.button_url,
'button_label': teaser.button_label,
'image_orientation': image_orientation
} only %}
{% endfor %}
使用and三值条件运算符,也可以使用如下函数:
{% set image_orientation_count = 1 %}
{% for mw_id in post.modeteaser_list %}
{% set teaser = TimberPost(mw_id) %}
{% if image_orientation_count is odd %}
{% set image_orientation = 'left' %}
{% else %}
{% set image_orientation = 'right' %}
{% endif %}
{% include 'templates/blocks/section-image-wide-text-small.twig' with {'title': teaser.title, 'copytext': teaser.copytext, 'mood_image': teaser.mood_image, 'button_url': teaser.button_url, 'button_label': teaser.button_label, 'image_orientation': image_orientation } only %}
{% set image_orientation_count = image_orientation_count + 1 %}
{% endfor %}
{% for ... %}
{% include "foo.twig" with {
"image_orientation": cycle(["left", "right"], loop.index0)
} only %}
{% endfor %}
您也可以使用如下功能:
{% set image_orientation_count = 1 %}
{% for mw_id in post.modeteaser_list %}
{% set teaser = TimberPost(mw_id) %}
{% if image_orientation_count is odd %}
{% set image_orientation = 'left' %}
{% else %}
{% set image_orientation = 'right' %}
{% endif %}
{% include 'templates/blocks/section-image-wide-text-small.twig' with {'title': teaser.title, 'copytext': teaser.copytext, 'mood_image': teaser.mood_image, 'button_url': teaser.button_url, 'button_label': teaser.button_label, 'image_orientation': image_orientation } only %}
{% set image_orientation_count = image_orientation_count + 1 %}
{% endfor %}
{% for ... %}
{% include "foo.twig" with {
"image_orientation": cycle(["left", "right"], loop.index0)
} only %}
{% endfor %}
非常感谢。这正是我所希望的!:)为你高兴,为你的归来感谢你!这正是我所希望的!:)为你高兴,为你的家人高兴return@Maerlyn:我不知道循环函数,所以谢谢你+1:)@Maerlyn:我不知道循环函数,所以谢谢你+1:)