Provide methods to traverse variables in Twig

Created on 13 September 2015, almost 9 years ago
Updated 9 March 2023, over 1 year ago

Currently it is not possible to access various properties of twig variables in templates which makes some operations huge pita.

For example have a look at Classy's menu-local-task.html.twig template:

<li{{ attributes.addClass(is_active ? 'is-active') }}>{{ link }}</li>

it is not possible to give the class to the link itself, instead we can only print the (global)attributes on their own and so to have the active class we can only give it to the list item.

Another example can be theming forms. For example you want to add a bootstrap or foundation class to your submit buttun but you cannot since you cannot access attributes of form.submit(for example). Instead you have to wrap such variable into another html element and assign class to it which won't work in this example since both of these frameworks require the class(alert, button, tiny...) to be applied to the form element directly.

I think Drupal should allow to traverse the variables(if the developer/themer knows the structure) in a way that allows manipulating the data before it is rendered. Since at this moment we have to use, in many many cases, the theme's theme file which basically means we are hard coding css classes(in this example) instead of making them part of the template as they should.

We have the NestedArray class so maybe some Twig extension could be built upon it.

✨ Feature request
Status

Active

Version

10.1 ✨

Component
Theme  →

Last updated 1 minute ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024