Home > database >  How to verify day of the week to show / hide content
How to verify day of the week to show / hide content

Time:03-31

I'm a bit stuck here and making it too complex in my head :-D

What I'd like to achieve and the current situation

I have a page, let's say product.twig which includes a countdown timer file (i.e. countdown.twig) which loads a JS script with the actual functionality.

I want to hide the countdown timer on specific days of the week (saturday & sunday), and instead show a custom text. I could make this work in JS, but this needs to run the script, which causes unnecessary load. The most efficiënt way would be to verify which day of the week it is in TWIG, so everything is done by the server, by not loading the countdown page, and thus not loading the actual JS file.

Can this be done with an IF statement? IF day = saturday (or 6 / 7) text: Delivered tuesday ELSE include countdown.twig

CodePudding user response:

You can just use the date filter along with "now" to get the current date

{% if "now"|date('N') in [ 6, 7, ] %}
    Delivered tuesday
{% else %}
    {% include "countdown.twig" %}
{% endif %}

demo

CodePudding user response:

Darkbee's solution works like a charm! Tweaked it a bit to make it even more efficient. As the countdown timer isn't needed on Saturday & Sunday, I don't need the ELSE statement. Instead I changed the IF in IF NOT ({% if "now"|date('N') not in [ 0, 7, ] %})

{% if "now"|date('N') not in [ 0, 7, ] %}
    <div >
        <span >Morgen in huis?</span><span style="padding-top: 4px;" > Je hebt nog:</span>
        <span style="background-color: #547A82; color: white; border-radius: 4px; width: 75px;"  id="countdownTimer"></span>
    </div>
<script src="{{ 'countdowner.js.rain' | url_asset }}"></script>
{% endif %}

For the ones staring at the extension of countdowner.js.rain: It's Lightspeed HQ's framework which is based on TWIG / DRAFT

  • Related