1

I have a Matrix block weeklyMenuItems, each item in the block has a dropdown with days of the week (Monday, Tuesday etc). Each entry will have a Matrix block with 7 items, matching the 7 days of the week.

In the template, I would like to start the loop from todays day but still include previous days in the loop, at the end. So, according to the example, whichever item.dayOfTheWeek matches {{ now|date('l') }}.

For instance, if today's day is Wednesday, the loop will output...

  • Wednesday
  • Thursday
  • Friday
  • Saturday
  • Sunday
  • Monday
  • Tuesday

Here is part of my code, the entries that are being looped are from an entries field of another channel menuSection but I don't think that matters in this instance.

{% for entry in craft.entries.section('menuSection').find() %}

    {% for menuItem in entry.menuEntries %}

        <h2>{{ menuItem.title }}</h2>

        <ul>
            {# Matrix blocks #}
            {% for item in menuItem.weeklyMenuItems %}
                <li>
                    <h3>{{ item.dayOfTheWeek }}</h3>
                    <p>{{ item.itemTitle }}</p>
                </li>
            {% endfor %}
        </ul>

    {% endfor %}

{% endfor %}

Any guidance will be welcomed, let me know if I can clarify anything!

Thanks

1

Do you really want to start the Matrix loop from the week day offset? Or do you just want to only start printing when that offset matches. The former seems like a bit of a micro-optimization.

For the latter, something like this completely untested code should do it:

{% for entry in craft.entries.section('menuSection').find() %}

    {% for menuItem in entry.menuEntries %}

        <h2>{{ menuItem.title }}</h2>

        <ul>
            {% set offset = false %}

            {# Matrix blocks #}
            {% for item in menuItem.weeklyMenuItems %}

                {% if item.dayOfTheWeek == now|date('l') or offset > 0 }}
                    {% set offset = loop.index0 %}

                    <li>
                        <h3>{{ item.dayOfTheWeek }} - Matched</h3>
                        <p>{{ item.itemTitle }} - Matched</p>
                    </li>
                {% endif %}
            {% endfor %}

            {% if offset == false %}
                {% set offset = 0 %}
            {% endif %}

            {# 2nd pass #}
            {% for item in menuItem.weeklyMenuItems|slice(0, offset) %}
                <li>
                    <h3>{{ item.dayOfTheWeek }} - Matched</h3>
                    <p>{{ item.itemTitle }} - Matched</p>
                </li>
            {% endfor %}
        </ul>

    {% endfor %}

{% endfor %}
  • I would like to start the Matrix loop from todays day, but still include previous days. To give you a little more context, this is a carousel showing menu items. So I still need the user to be able to see previous days items (as they are the same for months at a time). Thanks! – MikeeBee Jan 11 '17 at 7:31
  • Updated answer. – Brad Bell Jan 11 '17 at 23:23
  • Thanks Brad, that will mark the one that is todays date but doesn't change the print order. The list still starts from Monday, regardless. Would you be able to show me how to change the order please? – MikeeBee Jan 14 '17 at 1:48
  • Sorry, not following. Maybe edit the original question to show the output results you're looking for? – Brad Bell Jan 15 '17 at 4:32
  • Edited, hopefully that clarifies things. – MikeeBee Jan 15 '17 at 7:47
1

This is the solution I ended up with. Creating arrays of both:

  • The days in the week from and including today
  • The days in the week previous to today

And then looping through each in succession to produce the format I want. I also added some code to display "today" and "tomorrow" as the title.

I have no doubt there will be a more elegant solution but this did the job for me.

{% for entry in craft.entries.section('menuSection').find() %}

    <ul>

        {% set day = now|date('l') %}

        {% set daysFromToday = [] %}
        {% set previousDays = [] %}

        {% for item in menuItem.weeklyMenuItems %}
            {% if item.dayOfTheWeek == day %}
                {% set daysFromToday = daysFromToday|merge([item.id]) %}
                {% set day = day|date_modify('+1 day') %}
                {% set day = day|date('l') %}
            {% else %}
                {% set previousDays = previousDays|merge([item.id])  %}
            {% endif %}
        {% endfor %}

        {% set daysFromTodayString = daysFromToday|join(', ') %}
        {% set previousDaysString = previousDays|join(', ')  %}

        {% for item in menuItem.weeklyMenuItems.id(daysFromTodayString) %}

            <li>
                <h3>{% if loop.index == 1 %}today{% elseif loop.index == 2 %}Tomorrow{% else %}{{ item.dayOfTheWeek }}{% endif %}</h3>
                <p>{{ item.itemTitle }}</p>
            </li>

        {% endfor %}

        {% for item in menuItem.weeklyMenuItems.id(previousDaysString) %}

            <li>
                <div>
                    <h3>{{ item.dayOfTheWeek }}</h3>
                    <p>{{ item.itemTitle }}</p>
                </div>
            </li>

        {% endfor %}
    </ul>

{% endfor %}

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.