How to check the value of a field in TWIG? - drupal

Have created a field of type "List (text)" and I want to display a text according to the value of the field. How to do this ? Thank you
{% if store.field_professionnel_ets_statut %}
micro-entreprenneur
{% else if store.field_professionnel_ets_statut %}
entreprenneur
{% endif %}
https://ibb.co/MGS05ZB
The "field_professionnel_ets_statut" field can have the values :
micro-entreprise
entreprise
Here's what I want :
If the field with the value "micro-entreprise" display "micro-entreprenneur".
If the field with the value "entreprise" display "entreprenneur".

{% if "micro-entreprise" in store.field_professionnel_ets_statut %}
micro-entreprenneur
{% else if "entreprise" in store.field_professionnel_ets_statut %}
entreprenneur
{% endif %}
this may help!

Try this :) :
{% if store.field_professionnel_ets_statut === 'micro-entreprise' %}
micro-entreprenneur
{% elseif store.field_professionnel_ets_statut === 'entreprise' %}
entreprenneur
{% endif %}

Related

How to change "if" condition related to a page

I would like to include tab_system.twig file in two differents files : archive.twig and blog_list.twig to have the same html in those two files.
{% include "tab_system.twig" %}
In this tab_system.twig file I have a condition to add the active class :
{% if XXX %}active{% endif %}
But this condition must be different depending on which page the user is.
For blog_list.twig :
{% if loop.first %}active{% endif %}
For archive.twig :
{% if item.term_id == term_page.term_id %}active{% endif %}
I wrote this without success :
{% set addclass = '' %}
{% if is_blog_list %}
{% set addclass = '{% if loop.first %}active{% endif %}' %}
{% elseif is_archive %}
{% set addclass = '{% if item.term_id == term_page.term_id %}active{% endif %}' %}
{% endif %}
In tab_system.twig I have a tab system with menu from a part and content to the other part. I wrote a js loop to display the corresponding content. I need to add active class on the first link and first content tab in blog_list.twig file and to add active class to the link and content tab depending on which category the user is.
<div class="tab-menu-list">
{% for item in all_posts %}
<a href="#" class="tab-menu {{ addclass }}"</a>
{% endfor %}
</div>
<div class="tab-content-list">
{% for item in all_posts %}
<div href="#" class="tab-content {{ addclass }}"</div>
{% endfor %}
</div>
is_archive and is_blog_list are variables defined elsewhere. They work
How can I create a condition ? Thank you in advance.
As I'm assuming you are looping the records, I'd say don't overcomplicate things,
single.twig
{% for i in 1..5 %}
{% include 'article.twig' with { 'active' : loop.first, } %}
{% endfor %}
template_blog.twig
{% for item in items %}
{% include 'article.twig' with { 'active' : item.term_id == term_page.term_id, } %}
{% endfor %}
article.twig
<div class="articles__list tab-content{{ active ? ' active'}}">
foo
</div>
demo - just change the main template
If you really wanted to keep that for inside the article template, which I think is not a great idea, you could still use the variables is_archive and is_blog_list inside article, e.g.
article.twig
{% for item in items %}
{% if is_archive %}
{% set active = loop.first %}
{% elseif is_blog_list %}
{% set active = item.term_id == term_page.term_id %}
{% endif %}
<div class="articles__list tab-content{{ active ? ' active'}}">
{% endfor %}

With Twig and WordPress, how do I determine if a post is in a category?

Twig newbie here :) And I'm using WordPress.
In a .twig file, I can display the category.slug this way:
{% for category in post.terms('category') %}
{{ category.slug }}
{% endfor %}
But how do I use an if construct with the category.slug?
I.e., I want to display the category if the post is in the category in-the-news. This obviously doesn't work:
{% for category in post.terms('category') %}
{% if category.slug == in-the-news %}
In The News
{% else %}
Other Category
{% endif %}
{% endfor %}
This is twig version is 1.34. Fiddle here from Matteo: https://twigfiddle.com/89t9gr
This works. I think the issue in my localhost and Twig v1.34 was having no quotes around the category slugs. And, for me, {% for category in categories %} does not work and does not throw an error.
{% for category in post.terms('category') %}
{% if category.slug == "in-the-news" %}
In The News
{% elseif category.slug == "videos" %}
Video
{% else %}
All Else
{% endif %}
{% endfor %}

TWIG condition for full URL

This code does not work.
{% if url == 'example.com/categoty' %}
some text
{% endif %}
How to do this?
I assume you don't have url variable assigned. There is a global parameter for that:
{% if app.request.requestUri == '/categoty' %}
some text
{% endif %}
{% if 'mysite/news' in url %}
some text
{% endif %}

Symfony : "if" does not work in a twig template

I'm trying to validate the size of an array before print a value, but the if instruction doesn't work. Always pass thought the if.
This is my code:
{% set size = custodian.phoneNumbers|length %}
{% if size > 3 %}
{% block phone_number3 %}{{phoneNumbers[2].phoneNumber }}{% endblock %}
{% endif %}
size is equal to 2
I try with this code and does not work as well.
{% set size = true %}
{% if size == false %}
{{size}}
{% endif %}
Please help!!!
Thanks in advance.
I found the answers myself
The block should be outside of the if.
{% block phone_type3 %}
{% if size >= 3 %}
{{ custodian.phoneNumbers[2].phoneType.value }}:
{% else %}
:
{% endif %}
{% endblock %}
{% block phone_number3 %}
{% if size >= 3 %}
<b>{{ custodian.phoneNumbers[2].phoneNumber }}</b>
{% endif %}
{% endblock %}

For every 10 records print something using Symfony2 and Twig

I'm building a page where member addresses are printed to a sheet of labels, and once a sheet is full with 14 addresses on a page, I then want to put a page break after it so that the addresses can be printed on multiple sheets of paper.
How can I achieve this using Twig?
This is my current Twig code:
{% for row in data %}
<div class="label-page">
<div class="label-section L7163">
<p>{{row.FirstName}} {{row.Surname}}</p>
<p>{{row.AddressLine1}}</p>
{% if row.AddressLine2 != NULL or row.AddressLine2 != '' %}
<p>{{row.AddressLine2}}</p>
{% endif %}
{% if row.Town != NULL or row.Town != '' %}
<p>{{row.Town}}</p>
{% endif %}
{% if row.County != NULL or row.County != '' %}
<p>{{row.County}}</p>
{% endif %}
{% if row.Postcode != NULL or row.Postcode != '' %}
<p>{{row.Postcode}}</p>
{% endif %}
</div>
</div>
{% endfor %}
You can use loop.index in your twig loop which returns the current iteration of the loop and divisible by(num)
{% for row in data %}
{% if loop.index is divisible by(10) %}
/*your page break*/
{% endif %}
{% endfor %}

Resources