Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-13T08:20:31.023Z
Answers.LastActivityDate: 2016-04-13T08:20:31.023Z
Answers.UserDisplayName: Jamie Wade
Body: <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>
  
Tags: <templating><matrix><forloop>
Title: Is there a better way than stacked if else
ThreadText: # 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
  
  ```
    <div id="slider-with-blocks-1" class="royalSlider rsMinW">
      {% 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 %}
      <div class="rsContent slide3">
        <a class="rsImg" href="{{ Bild.url }}"></a>
      </div>
      {% elseif block.textCSlideTop is empty and block.textBSlideTop is empty and block.textASlideTop is empty %}
      <div class="rsContent slide3">
        <a class="rsImg" href="{{ Bild.url }}"></a>
        <div class="bContainer">
          <strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline">{{ block.headlineSlideTop }}</strong>
        </div>
      </div>
      {% elseif block.textCSlideTop is empty and block.textBSlideTop is empty %}
      <div class="rsContent slide4">
        <a class="rsImg" href="{{ Bild.url }}"></a>
        <div class="bContainer">
          <strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline">{{ block.headlineSlideTop }}</strong>
          <span data-move-effect="top" class="rsABlock txtCent">{{ block.textASlideTop }}</span>
        </div>
      </div>
      {% elseif block.textCSlideTop is empty %}
      <div class="rsContent slide1">
        <a class="rsImg" href="{{ Bild.url }}"></a>
        <div class="bContainer">
          <strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline">{{ block.headlineSlideTop }}</strong>
          <span data-move-effect="top" class="rsABlock txtCent">{{ block.textASlideTop }}</span>
          <span data-move-effect="bottom" class="rsABlock txtCent">{{ block.textBSlideTop }}</span>
        </div>
      </div>
      {% else %}
      <div class="rsContent slide1">
        <a class="rsImg" href="{{ Bild.url }}"></a>
        <div class="bContainer">
          <strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline">{{ block.headlineSlideTop }}</strong>
          <span data-move-effect="top" class="rsABlock txtCent">{{ block.textASlideTop }}</span>
          <span data-move-effect="bottom" class="rsABlock txtCent">{{ block.textBSlideTop }}</span>
          <span data-move-effect="left" class="rsABlock txtCent">{{ block.textCSlideTop }}</span>
        </div>
      </div>
      {% endif %}
      {% endfor %}
    </div>
  
  ```
  
  **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:
  
  ```
  <div id="slider-with-blocks-1" class="royalSlider rsMinW">
      {% for block in entry.slideitemtop %}
          {% set Bild = block.ImageSlideTop.first() %}
  
          <div class="rsContent slide{{ loop.index }}">
              <a class="rsImg" href="{{ Bild.url }}"></a>
              <div class="bContainer">
                  {% if block.headlineSlideTop|length %}
                      <strong data-move-effect="none" class="rsABlock txtCent blockSubHeadline">{{ block.headlineSlideTop }}</strong>
                  {% endif %}
                  {% if block.textASlideTop|length %}
                      <span data-move-effect="top" class="rsABlock txtCent">{{ block.textASlideTop }}</span>
                  {% endif %}
                  {% if block.textBSlideTop|length %}
                      <span data-move-effect="bottom" class="rsABlock txtCent">{{ block.textBSlideTop }}</span>
                  {% endif %}
                  {% if block.textCSlideTop|length %}
                      <span data-move-effect="left" class="rsABlock txtCent">{{ block.textCSlideTop }}</span>
                  {% endif %}
              </div>
          </div>
  
      {% endfor %}
  </div>
  
  ```
  
  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 }}`.
  
  
  ---
  
  