Score:0

Set a multiple twig condition in views

jp flag

In a field, I wan't to display only results iltered by an if statement like this. But actually nothing is displayed.

{% if  ( nid == '16' ) and ( node.field_video_externe | default ) %}
{{ field_video_externe }} 
{% endif %}

Thanks for tips.

EDIT: if I add a empty field to rewrite the entire output and accessing to the fields via token, I could do something like this, but the video for other nids aren't exclude as I would.

{% if (field_illus_administratif is not empty) and (nid != 16) %}
{{ field_illus_administratif }}
{{ else }}
{{ field_video_externe }} 
{% endif %}
<span class="center">{{ title }}</span>

with these tokens available/

{{ nid }} ==
{{ field_illus_administratif }} == Illustration du contenu
{{ field_video_externe }} == Ajouter video externe
cn flag
Does the filter work if you try just one of the two conditions?
sonfd avatar
in flag
I'm not sure you're using `default` correctly. My understanding is that you pass a value to it and the value is used when there's no value from whatever you're applying the filter to. E.g. `{{ some_variable|default('hello world') }}` would show the value of `some_variable`, if it's not empty, otherwise it'll show "hello world". See the [twig default filter docs](https://twig.symfony.com/doc/3.x/filters/default.html)
sonfd avatar
in flag
You should also print your variables above the if statement so you can confirm the values are what you'd expect. You say this is in a views template - it seems unlikely to me that any of those variables will be defined in a views template.
WebmasterPF avatar
jp flag
@sonfd I put this code into the field rewriting in Drupal's Views UI. Then I can use token because I select the node ID (exclude from display) as a dynamic class for the <li> .
WebmasterPF avatar
jp flag
@PatrickKenny This code provide blank result: {% if field_video_externe | default %} {{ field_video_externe }} {% endif %} . But the condition on the node id doesn't work.If I don't rewrite the result of the field the content is displayed but I need to display only the content from the node 16, not the others.
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.