1
votes

I'm trying to get all the days of the week with a for loop in my twig view.

Here's the thing I want to do :

    <table>
        <thead>
        </thead>
        <tbody>
            <tr><td>{{ "+0 day"|date("d M y") }}</td></tr>
            <tr><td>{{ "+1 day"|date("d M y") }}</td></tr>
            <tr><td>{{ "+2 day"|date("d M y") }}</td></tr>
            <tr><td>{{ "+3 day"|date("d M y") }}</td></tr>
            <tr><td>{{ "+4 day"|date("d M y") }}</td></tr>
            <tr><td>{{ "+5 day"|date("d M y") }}</td></tr>
            <tr><td>{{ "+6 day"|date("d M y") }}</td></tr>
        </tbody>
    </table>

This is what I tried so far :

[...]
{% for day in  0..6 %}
    <tr>
        <td>{{ ('"' ~ '+' ~ day ~ ' day' ~ '"')|date("d M y") }}</td>
    </tr>
{% endfor %}

but I get this error message :

An exception has been thrown during the rendering of a template ("DateTime::__construct(): Failed to parse time string ("+0 day) at position 0 ("): Unexpected character")

Any idea how I should do this please ?

1

1 Answers

1
votes

Simply remove the double quote (") character:

{% for day in  0..7 %}
    {% set myDate = '+' ~ day ~ ' day' %}
    <tr>
        <td>{{ myDate|date("d M y") }}</td>
    </tr>
{% endfor %}

See this working twgifiddle