Score:-1

If conditions in twig to check if field has a value

et flag

I have used this below code to check if the field has a value

{% if content.field_title is not empty %}
    <p>{{ content.field_title }}</p>
{% endif %}

But p tag is rendering even though the field doesn't have a value. Same happening for {% if content.field_title is not null %} as well and {{ dump(content.field_title) }} is not an empty array if the field has no value. So how to check empty condition in twig for a field.

Edited: Trying to use the content object instead node/paragraph/term object. Here Verify a field is not empty used node object.

Thanks in advance!

Score:0
ve flag

Quite a few ways to check. For content fields in Twig I generally prefer:

{% if content.field_title['#items'].getValue() %}
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.