I'm trying to create an opened/closed shop widget in Twig. This have to be done in the frontend since I don't have access to create backend functions (SaaS platform)
I got almost everything working except for one thing. When you fill in a value that's not equal to one of the statements then the server throws a 500 internal error
. Whatever I try I just can't see how to check if that particulary var is set.
To clarify:
I can set these options:
theme.monday
theme.tuesday
theme.wednesday
etc...
Let's say I fill theme.monday
with value 09.00-16.00
. Which means shop is open from 9am till 4pm. This works with below code. When I fill theme.monday
with value CLosED
then the code below works as well.
However when I fill theme.monday
with value Hello! blabla
then the server throws an internal error.
With my limited Twig knowledge I understand that this error comes from the var combine
. If something else is set as value then that split function won't work offcourse.
My question is how to handle this? So how can I make some sort of check that will work when theme.monday
is filled with something different then closed
or 09.00-16.00
?
{% set open = false %}
{% set now = 'now' | date('U') %}
{% set today = 'now' | date('l') | lower %}
{% set closedval = attribute(theme, today) | lower | t %}
{% if 'closed' in closedval %}
{% set open = false %}
{% else %}
{% set combine = attribute(theme, today) | split('-') %}
{% set opening_time = combine[0] | date('U') %}
{% set closing_time = combine[1] | date('U') %}
{% if (now < closing_time) and (now > opening_time) %}
{% set open = true %}
{% endif %}
{% endif %}
Things I tried are:
{% set open = false %}
{% set now = 'now' | date('U') %}
{% set today = 'now' | date('l') | lower %}
{% set closedval = attribute(theme, today) | lower | t %}
{% set combine = attribute(theme, today) | split('-') %}
{% if 'closed' in closedval %}
{% set open = false %}
{% elseif combine is not null %}
{# or
{% elseif combine is defined %}
{% elseif combine is not sameas(false) %}
#}
{% set opening_time = combine[0] | date('U') %}
{% set closing_time = combine[1] | date('U') %}
{% if (now < closing_time) and (now > opening_time) %}
{% set open = true %}
{% endif %}
{% else %}
{% set open = false %}
{% endif %}
However above won't work. I can't see what I'm doing wrong. Any help greatly appreciated.