Wholesale store - if statement not working - if-statement

I'm setting up a wholesale store but having multiple usergroups. The first usergroup rdg000141 works fine but after the elsif part, the code stops working.
{% if template contains 'rdg000141' or template contains 'wholesale' %}
{% if customer.tags contains 'rdg000141' and template contains 'rdg000141' %}
{{ content_for_layout }}
{% else %}
{% include 'wholesale-note' %}
{% elsif customer.tags contains 'wholesale' and template contains 'wholesale' %}
{{ content_for_layout }}
{% else %}
{% include 'rdg000141-note' %}
{% endif %}
{% else %}
{{ content_for_layout }}
{% endif %}
{% endif %}
Cheers

Related

How to display text if the field is empty?

How to display a text if the field is empty ?
I tried the following code but it does not work :
{% if content.field_description is not empty %}
{{ content.field_description }}
{% else %}
test
{% endif %}
If you have Twig Tweak installed, you can do the following:
{% if content['field_description'] | field_value != '' %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
When field is empty it is not coming with content variable
so you can simply check by isset
{% if content.field_description %}
{{ content.field_description }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
The below worked for me:
{% if content['field_description'] IS NOT EMPTY %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
Please note that it may vary based on the content type and fields you are dealing with.

how to do assignments for variable in django template tag

I want to do assignment in Django template tag for this code:
{% for ins in ob %}
{% if ins.heur = 'here' %}
a==1
some stuff ..
{% endif %}
{% endfor %}
{% if a!=1 %}
stuff
{% endif %}
You can use the {% with %} template tag.
Example:
{% with a=1 %}
{{ a }}
{% endwith %}

`with` template tag with logical comparison result as value

Is there a way to assign a logical value from a comparison to a variable in the current context?
I tried :
{% with hidden=forloop.counter > 4 %}
{% include "path/to/template.html" %}
{% endwith %}
And it didn't work because that causes a syntax error. But perhaps there is just a syntax I don't know?
So far I do something like this :
{% if forloop.counter > 4 %}
{% with hidden=True %}
{% include "path/to/template.html" %}
{% endwith %}
{% else %}
{% include "path/to/template.html" %}
{% endif %}
And it works, But it looks dirty to me.
The include template tag allows you to pass extra context
{% if forloop.counter > 4 %}
{% include "path/to/template.html" with hidden=True %}
{% else %}
{% include "path/to/template.html" %}
{% endif %}

How do I implement a recursive and extensible Twig template?

I'm implementing a simple website menu, but this time I'm using Twig as my template language. The depth of the menu tree is one or greater. Here's my Twig code so far (sanitized and simplified):
{# file menu1.html.twig #}
<ul>
{% import _self as renderer %}
{% for item in menu.items %}
{{ renderer.renderItem(item) }}
{% endfor %}
</ul>
{% macro renderItem(item) %}
{% block itemtag %}
<li>
{% endblock %}
{{ item.name }}
{% if item.hasItems() %}
<ul>
{% import _self as renderer %}
{% for subitem in item.items %}
{{ renderer.renderItem(subitem) }}
{% endfor %}
</ul>
{% endif %}
</li>
{% endmacro %}
Now I need to override the "itemtag" element in another template:
{# file menu2.html.twig #}
{% extends "menu1.html.twig" %}
{% block itemtag %}
<li data-foo="bar">
{% endblock %}
This will not work, as explained very well here: https://stackoverflow.com/a/26650103/220817
So how do I write a Twig template that can traverse and render a tree structure, and still allow extending templates to override certain elements in the rendered markup?
You need to use macro's if you want to do something recursive as macro's can self import themself. Here is an example of a recursive macro:
{% macro menu(m, class, currentLevel, maxLevel) %}
{%if not class is defined %}
{% set class = '' %}
{% endif %}
{%if not currentLevel is defined or currentLevel is null %}
{% set currentLevel = 1 %}
{% endif %}
{%if m is iterable%}
{% set links = m %}
{% else %}
{% set links = m.getLinks() %}
{% if m.showRoot() and m.hasRoot() %}
<li class="root{%if class != ''%} {{class}}{%endif%}">
<a href="{%if m.getRoot().getTreeLeft() > 1 %}{{ m.getRoot().getRoute() }}{% else %}#site_url#{% endif %}" {% if m.getRoot().PageId in selected_page_ids %} class="selected"{% endif %}>{{ m.getRoot().name }}</a>
</li>
{% endif %}
{% if maxLevel is null %}{% set maxLevel = m.getLevel() %}{% endif %}
{% endif %}
{% if not links is empty %}
{% for link in links %}
{% if link['selected'] is defined and link['selected'] %}
{% set is_selected = true %}
{% else %}
{% set is_selected = false %}
{% endif %}
{% set anchor_class = '' %}
{% if link.PageId in selected_page_ids or is_selected %}
{% set anchor_class='selected' %}
{% endif %}
{% if method_exists(link, 'getPageCssClass') and link.getPageCssClass() != null %}
{% set class= class~' '~link.getPageCssClass().getTitle() %}
{% endif %}
<li{%if (class|trim) != '' %} class="{{ (class|trim) }}"{% endif %}>
<a href="{{ link.getRoute }}"{%if (anchor_class|trim) != '' %} class="{{ (anchor_class|trim) }}" {% endif %} {% if link.PageId > 0 %}data-page-id="{{ link.PageId }}"{% endif %}>{{ link.name }}</a>
{% if link.hasPublicChildren and currentLevel < maxLevel %}
<ul>
{{ _self.menu(link.getPublicChildren, 'sec', (currentLevel+1), maxLevel) }}
</ul>
{% endif %}
</li>
{% endfor %}
{% endif %}
{% endmacro %}

Conditional variable assignment

I'm doing this in one of my templates:
{% if activite.travel %}
{% with activite.travel.personne as personne %}
{% include 'includes/person_detail.html' %}
{% endwith %}
{% endif %}
{% if activite.relation %}
{% with activite.relation.src as personne %}
{% include 'includes/person_detail.html' %}
{% endwith %}
{% endif %}
Note: I may have more fields to come in activite that's why I'm not doing "else" but two separate "if".
I'd like to do something like:
{% if activite.travel %}
{% set personne=activite.travel.personne %}
{% elsif activite.relation %}
{% set personne=activite.relation.src %}
{% endif %}
{% include 'includes/person_detail.html' %}
Is there a way to do this in the template?
Not exactly... but you can use with inside of include
{% if activite.travel %}
{% include 'includes/person_detail.html' with personne=activite.travel.personne %}
{% elif activite.relation %}
{% include 'includes/person_detail.html' with personne=activite.relation.src %}
{% endif %}