Home > Blockchain >  Get data of dynamic variable in twig
Get data of dynamic variable in twig

Time:11-16

Hope someone tried this before or knows how to achieve what I'm trying to do.

I'm trying to get the data of the following variable.

{% if openingHours %}
   {% set name = "openingHours."~ myDate|date('l') ~ "Start" %}
   {% set openingHour = attribute(openingHours, name) %}
{% endif %}

The result should be e.g. openingHour.MondayStart. That's the variable I want the data from.

But it keeps throwing an error about the Entity.

The Entity error

I already checked if the data is accessible in twig via dump. The whole data of the entity can be accessed. If put openingHour.MondayStart in twig (hard coded) it gives me my desired data. But since it's a loop for all weekdays, I want data from all fields.

CodePudding user response:

name in your template should resolve to MondayStart but currently it's openingHours.MondayStart which you try to access on openingHours so what you're currently trying to access is openingHours.openingHours.MondayStart

your set tag should be

{% set name = myDate|date('l') ~ 'Start' %}

or you could do what β.εηοιτ.βε suggests, but I guess you want that name for something else as well.

  • Related