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.


{{ 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


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

The raw filter marks the value as being "safe", which means that in an environment with automatic escaping enabled this variable will not be escaped if raw is the last filter applied to it

source: Twig's official docs

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

  • 6
    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
  • 2
    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
  • 2
    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
  • 2
    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
  • 2
    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 }}

etc. More info on projects page

{{ content.body | raw }} 

get the raw field value in a twig template

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.