我正在使用HubL,这是Hubspots限量版的Jinja . python或php中的服务器端编码不是Hubspot的选项,只是fyi .

我正在使用选择和循环来允许编辑页面的人选择他们想要提供给页面的背景图像的数量 .

{% set bg_num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] %}

{% choice "bg_num_choice" label="Number of Background Images", value="3", choices="{{ bg_num }}", export_to_template_context=True %}

{% for options in bg_num %}

     {% if loop.index <= widget_data.bg_num_choice.value %}

         {% image_src "bg_img" label="Background Image", unique_in_loop=True, export_to_template_context=True %}

     {% endif %}

{% endfor %}

如您所见,根据所选的数量,这将在页面上启用许多Image src模块 .

我遇到的问题是我现在需要随机选择其中一个(每次加载页面)并将其分配给div:

<div class="home_body_wrapper" style="background: url({{ Assign Here }});"> </div>

为此,我知道我需要收集创建到数组中的模块的src值,然后使用随机过滤器来打印我需要它的值 .

问题是我必须动态创建数组 . 我无法将数组值设置为静态,因为我要么具有空值,要么必须根据使用的图像数来更改数组 .

如何将这些模块的值动态添加到数组中?