6

I added a view template views-view-fields--nieuws--page.html.twig to my theme.

The template is correct, when I edit it I see the results on my website.

{% for field in fields %}
  {{ field.separator }}
  {{ field.wrapper_prefix }}
    {{ field.label_html }}
    {{ field.content }}
  {{ field.wrapper_suffix }}
{% endfor %}

In page.html.twig I print the region like this:

{{ page.content }}

But my output has HTML tags in it, any idea how I can solve this?

EDIT: It appears my HTML is rendering at all through Twig

{% set hello = '<strong>Hello</strong>' %}
{{ hello }}

outputs the <strong> tags, too.

1
  • raw filter: marks a variable as being safe. You try use {{ hello | raw }}.
    – rpayanm
    Mar 30, 2015 at 14:17

4 Answers 4

2

This is currently a known bug with the views-view-fields.html.twig template: https://www.drupal.org/node/2363423

2

None of the above solution worked in my case. Here how I solved it by coding

function mytheme_preprocess_views_view_field(&$variables) {
  if ($variables['view']->id() == 'my_viewname' && $variables['field']->field == 'field_description') {
    $output = $variables['field']->getValue($variables['row']);
    $variables['output'] = ['#markup'=> $output];
  }
}

In views-view-fields--my-viewname.html.twig just print the value. No need to autoscaping or raw filer

{{ fields.field_description.content }}
1

You need to unescape HTML. There are two working ways to accomplish this. First you can mark your field as "raw" and the html would render correctly. {{ your_field | raw }} should render ok.

As a second resort you can use twig's autoescape functionality:

{% autoescape false %}
{{ your_field }}
{% endautoescape %}

By this way you are telling twig not to escape html markup.

-2

You need to add |raw for print html {{ field.content|raw }} but you need to remove wrapper

{{ field.wrapper_prefix }} {{ field.wrapper_suffix }}

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