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

CC BY-SA 3.0
2015-06-10T18:53:02.627Z
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!
Christopher Healey
CC BY-SA 3.0
2015-06-10T18:26:35.743Z
2015-06-10T18:26:35.743Z
Bryan Redeagle
<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>

<macro><switch>
Switch in a Macro
# 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 %}

    
        
            
        
        {% for slide in block.slides %}
        
            
                {{ slide.slideTitle }}
            
        
        {% endfor %}
    
    
        {% for slide in block.slides %}
            
                
                    
                        
                            
                                
                                    {% for image in slide.slideImage %}
                                        
                                    {% endfor %}
                                
                            
                        
                    
                
                
                    
                        
                            {% if slide.heading|length %}
                                
                                    {{ slide.heading }}
                                
                            {% endif %}
                            {% if slide.intro|length %}
                                {{ slide.intro|replace({'' : ''})| raw }}
                            {% endif %}
                            {% if slide.text|length %}
                                {{ slide.text }}
                            {% endif %}
                        
                    
                
            
        {% endfor %}
    


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!  

---

