Add a heading() Twig function
#18518
-
|
Rendering correct heading hierarchies can be challenging when working with modular content, and has arguably become even more difficult as Craft 5 has freed developers up to nest, embed and So for a while now, I've used this pattern a lot: {{ include('some-partial.twig', {
heading: 'Great heading',
headingLevel: 3
}) }}
...
{# some-partial.twig #}
{% set headingLevel = headingLevel ?? 2 %}
{{ tag("h#{headingLevel}", {
class: 'text-large',
text: heading
} }}The idea here is that I can pass a This way, I can have a default heading level per partial (like in the above, where the default would be a The Craft already ships with some dedicated tag-rendering functions, like Personally, I think this would look pretty clean: {{ heading(headingLevel, {
class: 'text-large',
text: heading
}) }}
{# ☝️ ... would render <h2 class="text-large">Great heading</h2> #} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Good idea! Just added this for 5.10: #18524 |
Beta Was this translation helpful? Give feedback.
Good idea! Just added this for 5.10: #18524