4

I've racked my brain in all directions but still no solution. Maybe someone has some advice?

I have the following block in an ExpressJS app using Nunjucks as the templating engine.

{%- for course in courses -%}
    {%- for rcourse in report.courses -%}
        {%- if rcourse.id == course.id -%}
            <li {{ 'class="column-pre"' if loop.last else ''}}><span>Cool!</span></li>
        {%- else -%}
            <li></li>
        {%- endif -%}
    {%- endfor -%}
{%- endfor -%}

My problem: I need to break the inner report.courses loop when the conditional proves truth-y. Basically, the moment that I print the non-empty <li> line, I need to jump to the next iteration of the courses loop.

I know that Nunjucks does't have a break for loops like Jinja2, Nunjucks' variables are scoped (so I can't set a sentinel-like variable that gets modified in the if/else statement), nor can I append to an array so that I can use array.length as a way to determine if I should print the <li></li> line.

Maybe someone has a clever solution?

1 Answer 1

1

I think the sentinel-like variable would not be affected by scoping:

{% for course in courses %}
    {% set searchrcourse = true %}
    {% for rcourse in report.courses %}
        {% if searchrcourse %}
            {% if rcourse.id == course.id %}
                <li {{ 'class="column-pre"' if loop.last else ''}}><span>Cool!</span></li>
                {% set searchrcourse = false %}
            {% else %}
                <li></li>
            {% endif %}
        {% endif %}
    {% endfor %}
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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