Is there a better way than stacked if else 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

  &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;
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:

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


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