1

This question is based on the desire to have only one wrapping tag around the title value. (I am utilizing the block system to render the page/node title)

Both of the templates utilize some sort of attribute array on the wrapping element, so I am not sure it is safe to remove either.

I am hoping to better understand why there are two levels to this elements theming, and maybe more importantly, would it be detrimental to remove the attributes from the field--node--title.html.twig?

page-title.html.twig:

{{ title_prefix }}
{% if title %}
  <h1{{ title_attributes }}>{{ title }}</h1>
{% endif %}
{{ title_suffix }}

field--node--title.html.twig:

<span{{ attributes }}>
  {%- for item in items -%}
    {{ item.content }}
  {%- endfor -%}
</span>
2
  • Every page has a title, but not every page has title field (views pages, settings forms,...). Only fieldable entities (like node, user, taxonomy term,...) can use a field--ENTITYTYPE--FIELDNAME.html.twig template.
  • Very often the title field of an entity is NOT the title of a page (imagine a views page showing 10 nodes), that's why the h1 is in page-title.html.twig
  • Modules or field formatters might use those attributes. Removing it is no showstopper, but it might have side effects depending on the modules you use. (e.g. core's QuickEdit module will not work without {{ attributes }}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.