<p>There is certainly a lot of repeated code here so we can definitely improve on that! There are so many ways you could handle your Twig templates but this is how I would do it:</p>

<pre><code>&lt;div id="slider-with-blocks-1" class="royalSlider rsMinW"&gt;
    {% for block in entry.slideitemtop %}
        {% set Bild = block.ImageSlideTop.first() %}

        &lt;div class="rsContent slide{{ loop.index }}"&gt;
            &lt;a class="rsImg" href="{{ Bild.url }}"&gt;&lt;/a&gt;
            &lt;div class="bContainer"&gt;
                {% if block.headlineSlideTop|length %}
                    &lt;strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline"&gt;{{ block.headlineSlideTop }}&lt;/strong&gt;
                {% endif %}
                {% if block.textASlideTop|length %}
                    &lt;span data-move-effect="top" class="rsABlock txtCent"&gt;{{ block.textASlideTop }}&lt;/span&gt;
                {% endif %}
                {% if block.textBSlideTop|length %}
                    &lt;span data-move-effect="bottom" class="rsABlock txtCent"&gt;{{ block.textBSlideTop }}&lt;/span&gt;
                {% endif %}
                {% if block.textCSlideTop|length %}
                    &lt;span data-move-effect="left" class="rsABlock txtCent"&gt;{{ block.textCSlideTop }}&lt;/span&gt;
                {% endif %}
            &lt;/div&gt;
        &lt;/div&gt;

    {% endfor %}
&lt;/div&gt;
</code></pre>

<p>All we are doing here is using the same template code for each slide, but for each loop through we are checking if the 4 fields have any content or not.</p>

<p>I also noticed a <code>slide1</code> class. You can also automatically increment the number at the end of the class for each loop using <code>{{ loop.index }}</code>.</p>

CC BY-SA 3.0
2016-04-13T08:20:31.023Z
2016-04-13T08:20:31.023Z
Jamie Wade
<p>i am using a slider in my template, and depending on if the user filled out values in the craft matrix (slides can have up to 4 rows) i want to have twig compile only the necessary code. I am not sure if my attempt is way too long and it could be archived much easier. thanks a lot for the help</p>

<pre><code>  &lt;div id="slider-with-blocks-1" class="royalSlider rsMinW"&gt;
    {% for block in entry.slideitemtop %}
    {% set Bild = block.ImageSlideTop.first() %}
    {% if block.textCSlideTop is empty and block.textBSlideTop is empty and block.textASlideTop is empty and block.headlineSlideTop is empty %}
    &lt;div class="rsContent slide3"&gt;
      &lt;a class="rsImg" href="{{ Bild.url }}"&gt;&lt;/a&gt;
    &lt;/div&gt;
    {% elseif block.textCSlideTop is empty and block.textBSlideTop is empty and block.textASlideTop is empty %}
    &lt;div class="rsContent slide3"&gt;
      &lt;a class="rsImg" href="{{ Bild.url }}"&gt;&lt;/a&gt;
      &lt;div class="bContainer"&gt;
        &lt;strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline"&gt;{{ block.headlineSlideTop }}&lt;/strong&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    {% elseif block.textCSlideTop is empty and block.textBSlideTop is empty %}
    &lt;div class="rsContent slide4"&gt;
      &lt;a class="rsImg" href="{{ Bild.url }}"&gt;&lt;/a&gt;
      &lt;div class="bContainer"&gt;
        &lt;strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline"&gt;{{ block.headlineSlideTop }}&lt;/strong&gt;
        &lt;span data-move-effect="top" class="rsABlock txtCent"&gt;{{ block.textASlideTop }}&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    {% elseif block.textCSlideTop is empty %}
    &lt;div class="rsContent slide1"&gt;
      &lt;a class="rsImg" href="{{ Bild.url }}"&gt;&lt;/a&gt;
      &lt;div class="bContainer"&gt;
        &lt;strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline"&gt;{{ block.headlineSlideTop }}&lt;/strong&gt;
        &lt;span data-move-effect="top" class="rsABlock txtCent"&gt;{{ block.textASlideTop }}&lt;/span&gt;
        &lt;span data-move-effect="bottom" class="rsABlock txtCent"&gt;{{ block.textBSlideTop }}&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    {% else %}
    &lt;div class="rsContent slide1"&gt;
      &lt;a class="rsImg" href="{{ Bild.url }}"&gt;&lt;/a&gt;
      &lt;div class="bContainer"&gt;
        &lt;strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline"&gt;{{ block.headlineSlideTop }}&lt;/strong&gt;
        &lt;span data-move-effect="top" class="rsABlock txtCent"&gt;{{ block.textASlideTop }}&lt;/span&gt;
        &lt;span data-move-effect="bottom" class="rsABlock txtCent"&gt;{{ block.textBSlideTop }}&lt;/span&gt;
        &lt;span data-move-effect="left" class="rsABlock txtCent"&gt;{{ block.textCSlideTop }}&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    {% endif %}
    {% endfor %}
  &lt;/div&gt;
</code></pre>

<templating><matrix><forloop>
Is there a better way than stacked if else
# Is there a better way than stacked if else

(Asked by: username_0 on 2016-04-13)

i am using a slider in my template, and depending on if the user filled out values in the craft matrix (slides can have up to 4 rows) i want to have twig compile only the necessary code. I am not sure if my attempt is way too long and it could be archived much easier. thanks a lot for the help
  
    {% for block in entry.slideitemtop %}
    {% set Bild = block.ImageSlideTop.first() %}
    {% if block.textCSlideTop is empty and block.textBSlideTop is empty and block.textASlideTop is empty and block.headlineSlideTop is empty %}
    
      
    
    {% elseif block.textCSlideTop is empty and block.textBSlideTop is empty and block.textASlideTop is empty %}
    
      
      
        {{ block.headlineSlideTop }}
      
    
    {% elseif block.textCSlideTop is empty and block.textBSlideTop is empty %}
    
      
      
        {{ block.headlineSlideTop }}
        {{ block.textASlideTop }}
      
    
    {% elseif block.textCSlideTop is empty %}
    
      
      
        {{ block.headlineSlideTop }}
        {{ block.textASlideTop }}
        {{ block.textBSlideTop }}
      
    
    {% else %}
    
      
      
        {{ block.headlineSlideTop }}
        {{ block.textASlideTop }}
        {{ block.textBSlideTop }}
        {{ block.textCSlideTop }}
      
    
    {% endif %}
    {% endfor %}

**Tags:** templating, matrix, forloop


---

## 1 answers

### Answer by username_1 on 2016-04-13. Score: 5 | Accepted answer

There is certainly a lot of repeated code here so we can definitely improve on that! There are so many ways you could handle your Twig templates but this is how I would do it:

    {% for block in entry.slideitemtop %}
        {% set Bild = block.ImageSlideTop.first() %}

        
            
            
                {% if block.headlineSlideTop|length %}
                    {{ block.headlineSlideTop }}
                {% endif %}
                {% if block.textASlideTop|length %}
                    {{ block.textASlideTop }}
                {% endif %}
                {% if block.textBSlideTop|length %}
                    {{ block.textBSlideTop }}
                {% endif %}
                {% if block.textCSlideTop|length %}
                    {{ block.textCSlideTop }}
                {% endif %}
            
        

    {% endfor %}


All we are doing here is using the same template code for each slide, but for each loop through we are checking if the 4 fields have any content or not.
I also noticed a slide1 class. You can also automatically increment the number at the end of the class for each loop using {{ loop.index }}.


---

