Attributes
In the previous three examples of theme hooks, we encountered the concept of attributes
in the context of using them to render HTML elements. Attributes here are understood in the same way as with HTML. For example, class
, id
, style
, and href
are all HTML element attributes. Why is this important?
The reusability of theme hooks makes it so that we cannot hardcode all our HTML attributes in the Twig template files. We can have some, including classes, but we will always need to allow business logic to inform the theme hook of certain attribute values it needs printed on the HTML element. For example, an active
class on a link. This is why we have this concept of attributes.
Most theme hooks you’ll see have attributes in some form or another, with the variable usually being called $attributes
, $wrapper_attributes
, or something of that nature. Also, this variable always needs to be a multidimensional array with the attribute data you want passed. The keys in this...