2

I need to render a text (formatted long) field using twig. At this time i am doing the following below. However, i am finding that if i were to add a line break in the text field or apply adding spacing or a horizontal line, the twig filter "raw" removes those attributes. I need assistance to see what I am missing.

I have already set the view mode for field on the content type to default

Twig template (filter used)

{% for item in items %}
    {{ item.field_body.value|raw }}
{% endfor %}

Expecting enter image description here

Actual result enter image description here

5
  • You’re using an HTML enabled filter but then using raw. It won’t print. Why do you need full html if you’re going to remove it?
    – Kevin
    Oct 6, 2017 at 22:41
  • @Kevin for some reason i can only print it using {{ item.field_body.value|raw }}. if i just print .value then i get everything back but the html is returned as plaint text so when the page render it renders with <p> as if it were a plain text field. What is the appropriate way to print a full html field with all the applied styling in it? Oct 7, 2017 at 1:07
  • Leave it as the default? ie delete the custom twig file?
    – Kevin
    Oct 7, 2017 at 4:40
  • Or just do an {{ item.content }} ?
    – Hudri
    Oct 7, 2017 at 9:20
  • @Hudri i have set the view mode for the field as default. I need to use a twig template file to output specific <div> tags so using {{ item.content }} has not worked because for some reason it does not output as expected (see screenshots) Oct 8, 2017 at 19:53

1 Answer 1

3

This is the code I'm using inside one of my field templates to output a text field, which is used as an HTML/JS widget input box

{% for item in items %}
  <div{{ item.attributes }}>{{ item.content['#context'].value|raw }}</div>
{% endfor %}

Should work for you, I guess you'll have to remove the |raw filter for your case. (But usually it is not the recommended way to output fields this way.)

1
  • i didn't think to use the #context element. That works for me. Oct 13, 2017 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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