Answers.Body: <p>You can't nest a for loop within a <code>switch</code>, but outside of a <code>case</code>. Try this for brevity:</p>
  
  <pre><code>{% macro backgrounds(backgrounds, fieldName) %}
      {% for background in backgrounds %}
          {{ fieldName == background.value ? background.class : '' }}
      {% endfor %}
  {% endmacro %}
  </code></pre>
  
  <p>Also, your backgrounds array won't work sitting in the macro file. It needs to be either in the template, or included in from another template.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-10T18:53:02.627Z
Answers.Text: Thanks, i ended up realizing i was overcomplicating the entire thing and did not need a macro or a switch haha. Good to know for next time though!
Answers.UserDisplayName: Christopher Healey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-10T18:26:35.743Z
Answers.LastActivityDate: 2015-06-10T18:26:35.743Z
Answers.UserDisplayName: Bryan Redeagle
Body: <p>I'm getting the following error:</p>
  
  <p>Unexpected end of template. Twig was looking for the following tags "case", "default", or "endswitch" to close the "switch" block started at line 11)</p>
  
  <p>Here is the contents of my _macros template:</p>
  
  <pre><code>{% set backgrounds = {
      white: { class: "white-bg", value: "White" },
      grey: { class: "grey-bg", value: "Grey" },
      black: { class: "black-bg", value: "Black" },
      highlight: { class: "highlight-bg", value: "Highlight" },
      base: { class: "base-bg", value: "Base" }
      }
  %}
  
  {% macro backgrounds(backgrounds, fieldName) %}
      {% switch fieldName %}
          {% for background in backgrounds %}
              {% case "{{ background.value }}" %}
                  {{ background.class }}
          {% endfor %}
      {% endswitch %}
  {% endmacro %}
  </code></pre>
  
  <p>And here is my template that is throwing the error:</p>
  
  <pre><code>{% import '_macros' as siteMacros %}
  &lt;section class="content-35"&gt;
      &lt;ul class="content-35-customPager"&gt;
          &lt;li class="menuicon"&gt;
              &lt;span aria-hidden="true" class="fui-list"&gt;&lt;/span&gt;
          &lt;/li&gt;
          {% for slide in block.slides %}
          &lt;li&gt;
              &lt;a {% if loop.first %}class="active"{% endif %} data-slide-index="{{ loop.index0 }}"&gt;
                  {{ slide.slideTitle }}
              &lt;/a&gt;
          &lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
      &lt;ul class="content-35-slider"&gt;
          {% for slide in block.slides %}
              &lt;li class="
              content-35-slider-item 
              {{ siteMacros.backgrounds(backgrounds, slide.backgroundColor ) }}
              "&gt;
                  &lt;div class="background landscape"&gt;
                      &lt;div class="container"&gt;
                          &lt;div class="row"&gt;
                              &lt;div class="col-sm-4"&gt;
                                  &lt;div&gt;
                                      {% for image in slide.slideImage %}
                                          &lt;img src="{{ image.getUrl() }}" alt=""&gt;
                                      {% endfor %}
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="container"&gt;
                      &lt;div class="row"&gt;
                          &lt;div class="col-sm-5 col-sm-offset-5"&gt;
                              {% if slide.heading|length %}
                                  &lt;h2&gt;
                                      {{ slide.heading }}
                                  &lt;/h2&gt;
                              {% endif %}
                              {% if slide.intro|length %}
                                  {{ slide.intro|replace({'&lt;p&gt;' : '&lt;p class="lead"&gt;'})| raw }}
                              {% endif %}
                              {% if slide.text|length %}
                                  {{ slide.text }}
                              {% endif %}
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
  &lt;/section&gt;
  </code></pre>
  
  <p>The field slide.backgroundColor is a dropdown with all the values that are also listed in the Set for the backgrounds. Any help would be much appreciated.</p>
  
Tags: <macro><switch>
Title: Switch in a Macro
ThreadText: # Switch in a Macro
  
  (Asked by: username_0 on 2015-06-10)
  
  I'm getting the following error:
  
  Unexpected end of template. Twig was looking for the following tags "case", "default", or "endswitch" to close the "switch" block started at line 11)
  
  Here is the contents of my \_macros template:
  
  ```
  {% set backgrounds = {
      white: { class: "white-bg", value: "White" },
      grey: { class: "grey-bg", value: "Grey" },
      black: { class: "black-bg", value: "Black" },
      highlight: { class: "highlight-bg", value: "Highlight" },
      base: { class: "base-bg", value: "Base" }
      }
  %}
  
  {% macro backgrounds(backgrounds, fieldName) %}
      {% switch fieldName %}
          {% for background in backgrounds %}
              {% case "{{ background.value }}" %}
                  {{ background.class }}
          {% endfor %}
      {% endswitch %}
  {% endmacro %}
  
  ```
  
  And here is my template that is throwing the error:
  
  ```
  {% import '_macros' as siteMacros %}
  <section class="content-35">
      <ul class="content-35-customPager">
          <li class="menuicon">
              <span aria-hidden="true" class="fui-list"></span>
          </li>
          {% for slide in block.slides %}
          <li>
              <a {% if loop.first %}class="active"{% endif %} data-slide-index="{{ loop.index0 }}">
                  {{ slide.slideTitle }}
              </a>
          </li>
          {% endfor %}
      </ul>
      <ul class="content-35-slider">
          {% for slide in block.slides %}
              <li class="
              content-35-slider-item 
              {{ siteMacros.backgrounds(backgrounds, slide.backgroundColor ) }}
              ">
                  <div class="background landscape">
                      <div class="container">
                          <div class="row">
                              <div class="col-sm-4">
                                  <div>
                                      {% for image in slide.slideImage %}
                                          <img src="{{ image.getUrl() }}" alt="">
                                      {% endfor %}
                                  </div>
                              </div>
                          </div>
                      </div>
                  </div>
                  <div class="container">
                      <div class="row">
                          <div class="col-sm-5 col-sm-offset-5">
                              {% if slide.heading|length %}
                                  <h2>
                                      {{ slide.heading }}
                                  </h2>
                              {% endif %}
                              {% if slide.intro|length %}
                                  {{ slide.intro|replace({'<p>' : '<p class="lead">'})| raw }}
                              {% endif %}
                              {% if slide.text|length %}
                                  {{ slide.text }}
                              {% endif %}
                          </div>
                      </div>
                  </div>
              </li>
          {% endfor %}
      </ul>
  </section>
  
  ```
  
  The field slide.backgroundColor is a dropdown with all the values that are also listed in the Set for the backgrounds. Any help would be much appreciated.
  
  **Tags:** macro, switch
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-06-10. Score: 3 | Accepted answer
  
  You can't nest a for loop within a `switch`, but outside of a `case`. Try this for brevity:
  
  ```
  {% macro backgrounds(backgrounds, fieldName) %}
      {% for background in backgrounds %}
          {{ fieldName == background.value ? background.class : '' }}
      {% endfor %}
  {% endmacro %}
  
  ```
  
  Also, your backgrounds array won't work sitting in the macro file. It needs to be either in the template, or included in from another template.
  
  > **Comments:**
  > **username_0**: Thanks, i ended up realizing i was overcomplicating the entire thing and did not need a macro or a switch haha. Good to know for next time though!  
  
  ---
  
  