0
votes

I wanted to call images id with a variable like

<img src="{{ settings.custom_info_{{ i }}}_img1 | img_url: 'master' }}">

Obviously no handle are aloud inside a handle so I came with this solution. How can I clean my code so I don't have to make if for every element from the loop ?

<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>
1

1 Answers

1
votes

Here you can see a cleaner version of your code.

<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>

I see problems with your translatable strings, but you will figure that on your own.