3

When printing a text field we just want to display the value. When printed as is in a twig template ({{ content.field_subtitle }}) we get the following output.

Currently we are attempting not to use the |raw filter so we are just using {{ content.field_subtitle|render|striptags }}.

Are there alternatives to this approach without having to create another twig template for the field? We are having a hard time finding an alternative.

Setup:

  1. Basic page content type has field field_subtitle where type is text(plain)
  2. no twig template has been created on the field, only the content type node--page.html.twig

Current setup in twig template

<div class="myclass__subtitle>
  {{ content.field_subtitle|render|striptags }}
</div>

Current output:

<div class="myclass__subtitle">    
  <div class="field field--name-field-subtitle field--type-string field--label-hidden field__item">Intrinsicly harness interactive functionalities.</div>
</div>

Desired output:

<div class="myclass__subtitle">    
  Intrinsicly harness interactive functionalities
</div>
4

I am vehemently against things like render-array|render|striptags etc. It feels very unnatural and un-Drupal. What you should do is create a field template that has no markup.

For example, either in field.html.twig which would affect all fields, or in field--field-name.html.twig or more specific variants, you can do this:

{#
/**
* @file
* Theme override to remove all field markup.
*/
#}

{% spaceless %}
{% for item in items %}
  {{ item.content }}
{% endfor %}
{% endspaceless %}

This will always render that field with no markup.

| improve this answer | |
  • I agree about the striptags and raw filters, which is why I asked as I used the striptags to get by for now. So looks like the only option would be to create a field twig template for specific fields? – usernameabc Mar 15 '18 at 18:35
  • 1
    It’s bevwuse the markup is in the base field template. I usually start each project with a reset twig with the code above and fill in markup as needed in new templates. – Kevin Mar 15 '18 at 18:37
  • that makes sense. I inherited the website, but I wished they had started like that with a clean slate as you have shown. Then we could create twig templates when more markup was needed. – usernameabc Mar 15 '18 at 19:22
  • Yeah, then its just cleaner and you know to look for templates instead of random ways things got rendered. – Kevin Mar 15 '18 at 19:28
  • Worked great, thanks! Any idea how to name templates for field types to avoid code duplication? – glenviewjeff Jun 1 at 15:42
2

For a single value field, I often just do this: {{ content.field_subtitle.0 }}. This prints just the first delta, including whatever markup the field itself includes (e.g., works fine as well on reference fields or other more complex things) but does not use the field template at all.

| improve this answer | |

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.