我想用像
这样的变量调用图像id <img src="{{ settings.custom_info_{{ i }}}_img1 | img_url: 'master' }}">
显然手柄内没有手柄,所以我带来了这个解决方案。如何清理我的代码,以便我不必为循环中的每个元素制作?
<div class="custom_info__wrapper">
<div class="container">
<div class="row">
{% for i in (1..3) %}
{% capture text1 %}custom_info_{{ i }}_text1{% endcapture %}
{% capture text1_lang %}custom_blocks.custom_info_block_{{ i }}.text_1{% endcapture %}
{% capture text2 %}custom_info_{{ i }}_text2{% endcapture %}
{% capture text2_lang %}custom_blocks.custom_info_block_{{ i }}.text_2{% endcapture %}
{% capture img1 %}custom_info_{{i}}_img1{% endcapture %}
<div class="col-sm-4 custom_info custom_info__{{ i }}">
{% assign A = text1_lang | t %}{% if A.size > 0 %}<h6 class="heading_border-old">{{ A }}</h6>{% else %}<h6 class="heading_border-old">{{ settings[text1] }}</h6>{% endif %}
{% if i == 1 %}
<img src="{{ settings.custom_info_1_img1 | img_url: 'master' }}" />
{% elsif i == 2 %}
<img src="{{ settings.custom_info_2_img1 | img_url: 'master' }}" />
{% elsif i ==3 %}
<img src="{{ settings.custom_info_3_img1 | img_url: 'master' }}" />
{% endif %}
{% assign B = text2_lang | t %}{% if B.size > 0 %}<p>{{ B }}</p>{% else %}<p>{{ settings[text2] }}</p>{% endif %}
</div>
{% endfor %}
</div>
</div>
</div>
答案 0 :(得分:1)
在这里,您可以看到更清晰的代码版本。
<div class="custom_info__wrapper">
<div class="container">
<div class="row">
{% for i in (1..3) %}
{% capture text %}custom_info_{{ i }}_text{{ i }}{% endcapture %}
{% capture text_lang %}custom_blocks.custom_info_block_{{ i }}.text_{{ i }}{% endcapture %}
{% capture img %}custom_info_{{i}}_img{{ i }}{% endcapture %}
<div class="col-sm-4 custom_info custom_info__{{ i }}">
{% if forloop.index == 1 %}
{% assign A = text_lang | t %}{% if A.size > 0 %}<h6 class="heading_border-old">{{ A }}</h6>{% else %}<h6 class="heading_border-old">{{ settings[text] }}</h6>{% endif %}
{% endif %}
<img src="{{ settings[img] | img_url: 'master' }}" />
{% if forloop.index == 2 %}
{% assign B = text_lang | t %}{% if B.size > 0 %}<p>{{ B }}</p>{% else %}<p>{{ settings[text] }}</p>{% endif %}
{% endif %}
</div>
{% endfor %}
</div>
</div>
</div>
我看到你的可翻译字符串有问题,但你会自己想出来。