1

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.

3

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.

  • 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 Jun 10 '15 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.