Hello i would like do somthing like that:
<?php $count = 0; foreach($a as $v): $count++; ?>
<?php if ($count%2 == 0): ?>
...
<?php endif; ?>
<?php endforeach; ?>
in twig:
{% for v in a %}
{% if ??? is even %}
...
{% endif %}
{% endfor %}
but how can i have a variable evolving with loop ?
Apparently twig defines some loop variables inside the for-loop:
{% for v in a %}
{% if loop.index0 is even %}
...
{% endif %}
{% endfor %}
If you use it for styling you can do:
{% for v in a %}
<div class="link {{ cycle(['even', 'odd'], loop.index0) }}">
</div>
{% endfor %}
Related
My code is:
{% for key, value in section.items %}
{% for key_t, value_t in title.items %}
{% if value_t.section_id == key|add:"0" %}
<li class="nav-item-header"><div class="text-uppercase font-size-xs line-height-xs">
{{value.title}}</div> <i class="icon-menu" title="Tables"></i></li>
{% endif %}
{% endfor %}
{% endfor %}
I want to break the for loop when if the condition is true. like as
{% for key, value in section.items %}
{% for key_t, value_t in title.items %}
{% if value_t.section_id == key|add:"0" %}
<li class="nav-item-header"><div class="text-uppercase font-size-xs line-height-xs">
{{value.title}}</div> <i class="icon-menu" title="Tables"></i></li>
{{break}}
{% endif %}
{% endfor %}
{% endfor %}
How is it possible? please help me...
There is no way to break out of a for loop in Django Template. However, you can achieve this by setting a variable and adding an if statement on the top like this.
{% set isBreak = False %}
{% for number in numbers %}
{% if 99 == number %}
{% set isBreak = true %}
{% endif %}
{% if isBreak %}
{# this is a comment. Do nothing. #}
{% else %}
<div>{{number}}</div>
{% endif %}
{% endfor %}
for some additional help check out this link
https://dev.to/anuragrana/for-loops-in-django-2jdi
or check this answer on stack overflow
How to break "for loop" in Django template
I have an if/else condition in a twig template which switches the out tag of a block of code, however the inner block is the same. Is there a way to reduce the duplication without creating a separate file?
This is what I have at the moment:
{% if condition %}
<a href="">
{{ content }}
</a>
{% else %}
<span>
{{ content }}
</span>
{% endif %}
I was hoping to do something such as:
{% if condition %}
<a href="">
{% include mycontent %}
</a>
{% else %}
<span>
{% include mycontent %}
</span>
{% endif %}
{% mycontent %}
{{ content }}
{% endmycontent %}
Is such a thing possible?
If you don't want to use extra files you could use macro's :
{% import _self as macro %}
{% macro foo(content) %}
{{ content }}
{% endmacro %}
{% for condition in [0, 1, 0, 1, ] %}
{% if condition %}
{{ macro.foo('Bar') }}
{% else %}
<span>{{ macro.foo('Bar') }}</span>
{% endif %}
{% endfor %}
fiddle
What you want to do has to be done using the normal syntax. an extra file. and include this file.
But if u want to do this without extra file. use the {% set variablecontent = "put content here" %} and then in your "{% mycontent %}" part u put {{ variablecontent }}
hope this helps
I often have similar boilerplate code in Twig:
{% if (somelongcond) %}
<div>
{% endif %}
<p>Some long content</p>
{% if (somelongcond) %}
</div>
{% endif %}
The problem with the above is if the condition is changed, it can be a maintenance nightmare, I also have to go look all the way down to find the matching if statement and see if the condition is the same.
An alternative is something like this:
{% if (somelongcond) %}
<div>
{% include 'content' %}
</div>
{% endif %}
{% include 'content' %}
But that requires creating a new file, which can become a mess if I need to do this many times.
Is there a better way to do the above.
There is a good example here: https://gist.github.com/jakedohm/39190ec533e69e83b9cee4bdf3898a60
Result:
{% set content %}
<p>Some long content</p>
{% endset %}
{% if somelongcond %}
<div>
{{ content }}
</div>
{% else %}
{{ content }}
{% endif %}
This is a bit shorter
{{ if somelongcond ? '<div>'|raw }}
<p>Some long content</p>
{{ if somelongcond ? '</div>'|raw }}
Then, if the same condition is repeated, you can maybe set it at the top of your file, then if you need to change it you only have to do it once.
{% if somelongcond %}
{% set cond = true %}
{% else %}
{% set cond = false %}
{% endif %}
{{ if cond ? '<div>'|raw }}
<p>Some long content</p>
{{ if cond ? '</div>'|raw }}
I was going through the django docs and found this code in the template,
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
So could someone please explain what the {% %} tag stands for. Is it like <? ?> tag in php and if so why not add a single tag and print the rest of the html code?
I have
<div class="{{element.type == 'cover' ? 'cover-full' : 'fixed-width'}}">
<div class="element
{% if element.type == 'text' %}
element-text
{% elseif element.type =='image' %}
element-image
{% endif %}">
{% if element.type == 'text' %}
...
{% elseif element.type == 'image' %}
...
{% endif %}
</div>
As you can see I have to make the same if condition multiple times.
How can avoid to repeat every time the condition? I'm pretty new to Twig templating.
You will often have abit redundancy. But IMO it is a better practice to repeat HTML blocks instead of twig.
{% if element.type == 'text' %}
<div class="element element-text">
content
</div>
{% elseif element.type =='image' %}
<div class="element element-image">
content
</div>
{% else %}
{% include 'snippet.html' %}
{% endif %}
You have more valid and more readable HTML
If you change a condition you only have to change it one time
You could use {% include 'snippet.html' %} to reduce redundancy in HTML blocks
You can simplify your code:
{% if element.type in ['text','image'] %}element-{{element.type}}{% endif %}
You can use twig macro. On the top of that template add
{% macro element_class(type) -%}
{% if type in ['text','image'] %}element-{{type}}{% endif %}
{%- endmacro %}
{% from _self import element_class %}
And later:
<div class="{{element.type == 'cover' ? 'cover-full' : 'fixed-width'}}">
<div class="element {{ element_class(element.type) }}">
You can learn more about macros in Twig documentation.