Score:0

How to get value from object in twig template file?

mx flag

I can print the result object by {{ dump(item.value['#result']) }} in theme item-list--search-results.html.twig template file.

enter image description here

I want to print the body field value("body"=>"value") from the object.

How I will do ? I have tried some other ways which I know

  • {{ dump(item.value['#result']['node'].#values['body'][0]['value']) }}
  • {{ dump(item.value['#result']['node']['#values']['body'][0]['value']) }}
Score:3
cn flag

For normal fields you can get the field value by appending the property name .value:

{% set node = item.value['#result']['node'] %}

{{ node.field_foo.value }}

For the body field you need an extra step to process the stored text format:

{{ {
  '#type': 'processed_text',
  '#text': node.body.value,
  '#format': node.body.format,
} }}
Ramalingam Perumal avatar
mx flag
It's worked for me, thanks!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.