How to cycle across 2 forloops within a django template - django

I have a django template where I need to cycle through a set of background colours across two different for loops. The cycle tag seems to be designed to be used either within one for loop or outside a for loop altogether. This is my code:
{% if global_adverts %}
<span style="display:none">{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolors %}</span>
{% for advert in global_adverts %}
<div class="{% cycle adcolors %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
{% if adverts %}
{% for advert in adverts %}
<div class="{% cycle adcolors %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% endwith %}
Is there a way to do this without outputting the first item in the cycle before the first loop and having to hide it with css?

Just add silent
{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolors silent %}
It prevents the tag from outputting the value
EDIT AFTER COMMENT
If you need the cycle to continue for each loop just use cycle twice and change the order of the items in the second cycle:
{% if global_adverts %}
{% for advert in global_adverts %}
<div class="{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
{% if adverts %}
{% for advert in adverts %}
<div class="{% cycle 'advert-pale-blue' 'advert-green' 'advert-blue' 'advert-grey' %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% endwith %}

I have discovered that the following code gives the desired result:
{% if global_adverts %}
{% for advert in global_adverts %}
<div class="{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolours %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
{% if adverts %}
{% for advert in adverts %}
<div class="{% cycle adcolours %}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% endfor %}
{% endif %}
{% endwith %}

I feel that having the first {% cycle %} declaration outside of the loops is cleaner, I wouldn't have expected a declaration inside of conditional loops to work, and here's how to make it work:
{% cycle 'advert-grey' 'advert-pale-blue' 'advert-green' 'advert-blue' as adcolour silent %}
{% if global_adverts %}
{% for advert in global_adverts %}
<div class="{{ adcolours }}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% cycle adcolours %}
{% endfor %}
{% endif %}
{% with self.adverts.all as adverts %}
{% if adverts %}
{% for advert in adverts %}
<div class="{{ adcolours }}">
{% if advert.url %}<a href="{{ advert.url }}">{% endif %}
<p>{{ advert.text }}</p>
{% if advert.url %}</a>{% endif %}
</div>
{% cycle adcolour %}
{% endfor %}
{% endif %}
{% endwith %}
Since {% cycle adcolour %} is silent, we use {{ adcolour }} to print the current colour. We still need to use the silent {% cycle adcolour %} to step through the colours on each iteration.

Related

Validation of checkbox in Shopware 6 only working upon up and down scroll

I moved the terms and conditions consent from the original twig template so that it shows at a different position on the website. My problem is that when the consent checkbox is not checked, a warning should be shown to the user when clicking the payment button. This however only happens, when scrolling up and down on the website after clicking the finish payment button. Does anyone see my mistake?
{% sw_extends '#Storefront/storefront/page/checkout/confirm/index.html.twig' %}
{% block base_header %}
{% sw_include '#Storefront/storefront/layout/header/header-minimal.html.twig' %}
{% endblock %}
{% block base_navigation %}{% endblock %}
{% block page_checkout_main_content %}
{% block page_checkout_confirm %}
{% block page_checkout_confirm_header %}
<h1 class="confirm-main-header">
{{ "checkout.confirmHeader"|trans|sw_sanitize }}
</h1>
{% endblock %}
{% block page_checkout_confirm_alerts %}
{% block page_checkout_confirm_violations %}
{% for violation in formViolations.getViolations() %}
{% set snippetName = "error.#{violation.code}" %}
{% set fieldName = violation.propertyPath|trim('/', 'left') %}
{% sw_include '#Storefront/storefront/utilities/alert.html.twig' with {
type: "danger",
content: snippetName|trans({'%field%': fieldName})|sw_sanitize
} %}
{% endfor %}
{% endblock %}
{% endblock %}
{% block page_checkout_confirm_address %}
<div class="confirm-address">
{% sw_include '#Storefront/storefront/page/checkout/confirm/confirm-address.html.twig' %}
</div>
{% endblock %}
{% block page_checkout_confirm_payment_shipping %}
<div class="confirm-payment-shipping">
<div class="row">
{% block page_checkout_confirm_payment %}
<div class="col-sm-6 confirm-payment">
{% sw_include '#Storefront/storefront/page/checkout/confirm/confirm-payment.html.twig' %}
</div>
{% endblock %}
{% block page_checkout_confirm_shipping %}
<div class="col-sm-6 confirm-shipping">
{% sw_include '#Storefront/storefront/page/checkout/confirm/confirm-shipping.html.twig' %}
</div>
{% endblock %}
</div>
</div>
{% endblock %}
{% block page_checkout_confirm_product_table %}
<div class="confirm-product">
{% block page_checkout_confirm_table_container %}
<div class="card">
<div class="card-body">
{% block page_checkout_confirm_table_header %}
{% sw_include '#Storefront/storefront/page/checkout/confirm/confirm-product-header.html.twig' %}
{% endblock %}
{% block page_checkout_confirm_table_items %}
{% for lineItem in page.cart.lineItems %}
{% block page_checkout_confirm_table_item %}
{% sw_include '#Storefront/storefront/page/checkout/confirm/confirm-item.html.twig' %}
{% endblock %}
{% endfor %}
{% endblock %}
</div>
</div>
{% endblock %}
</div>
{% endblock %}
{% block page_checkout_confirm_tos %}
<div class="confirm-tos">
<div class="card checkout-card">
<div class="card-body">
{% block page_checkout_confirm_tos_header %}
<div class="card-title">
{{ "checkout.confirmTermsHeader"|trans|sw_sanitize }}
</div>
{% endblock %}
{% block page_checkout_confirm_revocation_notice %}
{% if config('confirm.revocationNotice') %}
<p class="revocation-notice">
{% block page_checkout_confirm_revocation_notice_link %}
<a href="{{ path('frontend.cms.page',{ id: config('core.basicInformation.revocationPage') }) }}"
{{ dataBsToggleAttr }}="modal"
title="{{ "checkout.confirmRevocationNotice"|trans|striptags }}"
data-url="{{ path('frontend.cms.page',{ id: config('core.basicInformation.revocationPage') }) }}">
{{ "checkout.confirmRevocationNotice"|trans|sw_sanitize }}
</a>
{% endblock %}
</p>
{% endif %}
{% endblock %}
{% block page_checkout_confirm_tos_control %}
<div class="{{ formCheckboxWrapperClass }}">
{% block page_checkout_confirm_tos_control_checkbox %}
<input type="checkbox"
class="checkout-confirm-tos-checkbox {{ formCheckInputClass }}{% if formViolations.getViolations('/tos') is not empty %} is-invalid{% endif %}"
required="required"
id="tos"
form="confirmOrderForm"
name="tos"/>
{% endblock %}
{% block page_checkout_confirm_tos_control_label %}
<label for="tos"
class="checkout-confirm-tos-label custom-control-label">
{{ "checkout.confirmTerms"|trans({
'%url%': path('frontend.cms.page',{ id: config('core.basicInformation.tosPage') })
})|raw }}
</label>
{% endblock %}
</div>
{% endblock %}
</div>
</div>
</div>
{% endblock %}
{% block page_checkout_confirm_hidden_line_items_information %}
{% sw_include '#Storefront/storefront/component/checkout/hidden-line-items-information.html.twig' with {
cart: page.cart,
lineItems: page.cart.lineItems
} %}
{% endblock %}
{% endblock %}
{% endblock %}
{% block page_checkout_additional %}
{% if config('core.cart.showCustomerComment') %}
<div class="checkout-additional">
{% block page_checkout_finish_customer_comment %}
<div class="card checkout-card">
<div class="card-body">
{% block page_checkout_confirm_customer_comment_header %}
<div class="card-title">
{{ "checkout.customerCommentHeader"|trans|sw_sanitize }}
</div>
{% endblock %}
{% block page_checkout_confirm_customer_comment_control %}
<div class="checkout-customer-comment-control">
{% block page_checkout_confirm_customer_comment_control_textfield_label %}
<label class="form-label" for="{{ constant('Shopware\\Core\\Checkout\\Order\\SalesChannel\\OrderService::CUSTOMER_COMMENT_KEY') }}">
{{ "checkout.customerCommentLabel"|trans|sw_sanitize }}
</label>
{% endblock %}
{% block page_checkout_confirm_customer_comment_control_textfield %}
<textarea class="form-control"
placeholder="{{ "checkout.customerCommentPlaceholder"|trans|sw_sanitize }}"
id="{{ constant('Shopware\\Core\\Checkout\\Order\\SalesChannel\\OrderService::CUSTOMER_COMMENT_KEY') }}"
form="confirmOrderForm"
name="{{ constant('Shopware\\Core\\Checkout\\Order\\SalesChannel\\OrderService::CUSTOMER_COMMENT_KEY') }}"></textarea>
{% endblock %}
</div>
{% endblock %}
</div>
</div>
{% endblock %}
</div>
{% endif %}
{% endblock %}
{% set formAddHistoryOptions = {
entries: [{
state: {},
title: 'account.ordersTitle'|trans,
url: path('frontend.account.order.page')
}]
} %}
{% block page_checkout_aside_actions %}
<div class="checkout-aside-action">
<form id="confirmOrderForm"
action="{{ path('frontend.checkout.finish.order') }}"
data-form-csrf-handler="true"
data-form-preserver="true"
data-form-submit-loader="true"
data-form-add-history="true"
data-form-add-history-options='{{ formAddHistoryOptions|json_encode }}'
method="post">
{% block page_checkout_aside_actions_csrf %}
{{ sw_csrf('frontend.checkout.finish.order') }}
{% endblock %}
{% block page_checkout_confirm_form_submit %}
{# #deprecated tag:v6.5.0 - Bootstrap v5 removes `btn-block` class, use `d-grid` wrapper instead #}
{% if feature('v6.5.0.0') %}
<div class="d-grid">
<button id="confirmFormSubmit"
class="btn btn-primary btn-lg"
form="confirmOrderForm"
{% if page.cart.errors.blockOrder %}
disabled
{% endif %}
type="submit">
{{ "checkout.confirmSubmit"|trans|sw_sanitize }}
</button>
</div>
{% else %}
<button id="confirmFormSubmit"
class="btn btn-primary btn-block btn-lg"
form="confirmOrderForm"
{% if page.cart.errors.blockOrder %}
disabled
{% endif %}
type="submit">
{{ "checkout.confirmSubmit"|trans|sw_sanitize }}
</button>
{% endif %}
{% endblock %}
</form>
</div>
{% endblock %}
{% block base_footer %}
{% sw_include '#Storefront/storefront/layout/footer/footer-minimal.html.twig' %}
{% endblock %}

Unable to add different block tag to my template

I am trying to add different block tags inside a for loop but it raise an error
Did you forget to register or load this tag?
But I register it
{% for todo in todo_list %}
{% if todo.complete %}{% else %}
{{todo.text|capfirst|truncatechars:150}} </a> <br>
<small class="text-muted">{{todo.content|capfirst}}{% empty %} {% endif %} </small> <hr>
{% endif %} {% endfor %}
Thanks
Looking into your problem, I think you need to try this:
{% for todo in todo_list %}
{% if todo.complete %}
{% else %}
{{todo.text|capfirst|truncatechars:150}} </a> <br>
{% if todo.content %}
<small class="text-muted">{{todo.content|capfirst}} </small> <hr>
{% else %}
//do something
{% endif %}
{% endif %}
{% endfor %}

How to use django paginator page range for displaying the first 10 numbers not all?

I want to change the blow script to show just the first 10 page numbers. Now it shows all page numbers. Could you please tell me how to change it? I tried several ways but it didn't work. Any help will be appreciated!
{% if forloop.counter|divisibleby:"3" or forloop.last %}
</div>
{% endif %}
{% endfor %}
{% if is_paginated %}
<div class="row">
<ul class="pagination pagination-md ">
{% if page_obj.has_previous %}
<li>«</li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }}</span></li>
{% else %}
<li><a href="?page={{ i }}{% if currentCategory %}&category={{ currentCategory }}
{% endif %}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li>»</li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
</div>
{% endif %}
</div>

Pagination on results doesn't work 'afte the first page' other papes repeats the first

I have an issue with pagination in Django search.html page. After search result, the results on first page of the table is repeated on the next pages continously.
For reference, this is the bit of the template that displays results:
{% for result in page_obj.object_list %}
<p>
{{ result.object.title }}
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page_obj.has_previous or page_obj.has_next %}
<div>
{% if page_obj.has_previous %}{% endif %}« Previous{% if page_obj.has_previous %}{% endif %}
|
{% if page_obj.has_next %}{% endif %}Next »{% if page_obj.has_next %}{% endif %}
</div>
{% endif %}

How do I get odd and even values in a Django for loop template?

I have this code
{% for o in some_list %}
Now I want to do some stuff if I am on an even line. How can I do that?
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#divisibleby
{% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %}
In first level cycle:
{% cycle 'odd' 'even' %}
Reference:
Documentation for cycle template tag
<div class="row">
{% for post in posts %}
{% cycle 'odd' 'even' %}
{% if cycle == 'odd' %}
<div class="col-md-6">Odd posts</div>
{% else %}
<div class="col-md-6">Even posts</div>
{% endif %}
{% endfor %}
</div>
OR
<div class="row">
{% for post in posts %}
{% if forloop.counter|divisibleby:2 %}
<div class="col-md-6">Even posts</div>
{% else %}
<div class="col-md-6">Odd posts</div>
{% endif %}
{% endfor %}
</div>
<div class="row">
{% for post in posts %}
{% if loop.index is divisibleby 2 %}
<div class="col-md-6">Even posts</div>
{% else %}
<div class="col-md-6">Odd posts</div>
{% endif %}
{% endfor %}
</div>
http://mitsuhiko.pocoo.org/jinja2docs/html/templates.html#id3