How do I get the raw field value in a twig template?

Solutions

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

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

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 &amp; and then &amp;amp; – Berdir

None of the suggestions so far worked for my text field. My text field is not multivalue, just a simple text field. Here is what did work for me:

content.field_myfieldname['#items'].getString()
Tags: Drupal 8 / Theming

Similar questions

Can I get the raw value of arbitrary tokens?
This site I'm working on is using tokens in such a way that the escaped (check_plain()'d) values provided by the tokens as is is problematic (they're getting double-escaped). Is there some way to specify that I want the raw values for those tokens? I tried just appending ":raw" to the token name, but that didn't seem to work. I could just write my ...
Get raw value of a block content
I have created several views. These views have a block display. If I use these blocks in my template, the views are rendered with all the wrappers / divs. In my block template, can I alter the output of the "content" variable to get only the values? I tried to use different twig filters (striptags, trim, raw), without success. With these filters I ...
How to print text (formatted, long) in twig without raw filter?
We have a text (formatted, long) field that is used to insert embed code such as the Youtube embed code for videos. When printing the field in twig we are finding that rather than rendering the embed code, it is displaying the embed code as string. To get around this, we currently apply Twig's Raw filter, but wanted to see if there was a way to not...
How do I use Twig to create raw, working html URL's in a Drupal 8 View?
The ProblemFrom within a custom view template I'm trying to output a twig variable as raw data inside a <a href=""> tag but it does not output anything? What I have tried I've tried using the Twig Tweak plugin, following the instructions on their cheat sheet (https://www.drupal.org/docs/8/modules/twig-tweak/cheat-sheet-8x-2x) but that doesn't...
Allow raw filter to work on label variable in twig's block view
What I'm trying to achieve is to set the raw filter on my label element so that if in the CMS the user decide to put HTML tags, it works. When I try to do it, it doesn't seem to do anything (it stills flushes the HTML tag). I'm wondering if the label variable is pre-processed by Drupal in xxx.theme? Anyways, if you guys have any idea on how I could...
How can I access raw field values in a view template?
I am writing a template for a views field in Drupal 8. How can I access the raw values of a multi-value field in views? I tried this but it doesn't work.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.