myfield is a plain text field. {{ content.field_myfield }} renders the field with HTML markup and the field label.

How do I render the raw value of the field? {{ content.field_myfield.value }} doesn't work.

up vote 26 down vote accepted

{{ entity.field_name.value }} to get the true raw value, includes tags and encoding.

  • Example: {{ node.body.value }}
  • result: <p>Batman &amp; Robin</p>

{{ content.field_name.0 }} to get the raw value minus tags and encoding.

  • Example: {{ content.body.0 }}
  • result: Batman & Robin

raw

This filter should be avoided whenever possible, particularly if you're outputting data that could be user-entered. See this page for more information on auto-escape in Drupal 8.

source: Filters - Modifying Variables In Twig Templates


For example, you can use:

{{ node.body.value|striptags }}
{{ paragraph.field_text.value|striptags }}

The problem with using twig's |striptags is double encoding of html entities, not markup, so & becomes & and then &amp; – Berdir

  • 5
    the content examples are not correct. See drupal.stackexchange.com/questions/198694/…. What you can use is content.field_text.0, then you only get the inner part of the first field item, without the field templates. That's especially useful for lists, references and so on, where you want the formatted output, not the raw value – Berdir Feb 13 '17 at 7:21
  • 1
    Better, but still not quite correct. content.field_myfield|striptags doesn't work, you can't pass a render array to that. You would need to render it first and then pass it through that. striptags and so on also have problems with & and other characters that can get encoded twice, so you oftne need a |striptags|raw actually. – Berdir Feb 13 '17 at 12:35
  • 1
    Just |raw leaves the html tags and prints them. The problem with |striptags is then double encoding of html entities, not markup, so & becomes &amp; and then &amp;amp; – Berdir Feb 13 '17 at 13:40
  • 1
    This is not double escaping, you can see that < and > are displayed correctly. The ampersand is probably already single escaped before you output it. But the question is about a plain text field, not a formatted text field processed by text filters. (To answer this not asked question, the raw value (if someone really wants that) would still be in .value and it would be correctly single escaped by Twig, for security reasons, because you never should output a formatted text field unfiltered) – 4k4 Dec 27 '17 at 9:32
  • 1
    Yes, this is what I meant (auto-escaping = single escaping). So your examples for outputting entity field values are perfect, only without the striptag filter, because this would produce the ugly double escaped output. (BTW the &amp; output in your example is wrong, this doesn't happen in autoescaping unless the field content is already escaped one time). – 4k4 Dec 27 '17 at 10:21

You can use Twig Field Value module in this case. After install this module You will get access to partial data from field render arrays. For ex.

{{ content.field_name|field_value }}
content.field_image|field_target_entity.uri.value

etc. More info on projects page

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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