Reputation: 33
New to twig and haven't found a great answer so far on this one:
I have an associative array of TimberMenus in the Timber context and each one corresponds to a different WordPress menu via the menu id.
In the Twig file I want a dynamic check to select which menu should be displayed, and evaluate the menu_name
variable.
{% if menus.{{menu_name}}.items %}
So if menu_name
= 'academics', I'd like the above code to evaluate to:
{% if menus.academics.items %}
The above doesn't work and I can't seem to find how I would accomplish this within Twig.
Any ideas or help very welcome!
Upvotes: 3
Views: 2317
Reputation: 4244
There is attribute
function, that allow to access dynamic properties:
{% if attribute(menus, menu_name).items %}
Upvotes: 2