i found around here a similar question like this one but the answers from there didn't help me.
<table>
<tr>
<th>Permission</th>
{% for role in roles %}
<th>{{role}}</th>
{% endfor %}
</tr>
{% for permission in permissions %}
{% if permission.PermissionName != currPerm %}
{% if currPerm != '' %}
<tr>
<td class="current-permission">{{currPerm}}</td>
{% for perm in permArray %}
<td>{{perm}}</td>
{% endfor %}
</tr>
{% endif %}
{% set currPerm = permission.PermissionName %}
{% set permArray = default %}
{% endif %}
{% set permArray[permission.RoleId] = '✓' %}
{% endfor %}
<tr>
<td>{{currPerm}}</td>
{% for perm in permArray %}
<td>{{perm}}</td>
{% endfor %}
</tr>
</table>
The code above contains this line
{% set permArray[permission.RoleId] = '✓' %}
Where i have problems setting the value for each key that it contains i tried with merge but with any luck it only adds a new key to the array and that's all.
If you have any other advices for this code i'm all ears.
With Twig you are not allowed to assign a value to an array key like this.
You must use the merge filter to do something like :
{% set permArray[permission.RoleId] = permArray|merge({permission.RoleId:'✓'}) %}
Related
I need to generate unique id in each loop instead of ````city-selected```
{% for form in formset.forms %}
<tr>
{% for field in form %}
<td class="input_td{% if field.errors %} error_td{% endif %}">
<select name="city-select" id="city-select"></select>
</td>
{% endfor %}
<td class="delete_formset_td"></td>
</tr>
{% endfor %}
How can I generate it here?
I need some thing like this for ids:
output:
city-1
city-2
city-3
...
You can use {{ forloop.counter }}. It gives you the loop iteration as a number.
See here.
{% for field in form %}
<!-- your html -->
city-{{ forloop.counter }}
{% endfor %}
I'd like to use a {% for %} loop in an included file, to avoid repeating logic to create the loop array (it's an assign with multiple where_exps).
But I'd like to use different content depending on where I include the loop, so sometimes I will do this:
{% for i in a %}
<li>{{ i.name }}</li>
{% endfor %}
and sometimes:
{% for i in a %}
<loc>{{ i.url }}</loc>
{% endfor %}
How can I achieve this? So far I have to put each of the inner contents in their own template, so I would have files like below, but I'd like to avoid the extra template files, and just keep that content in the appropriate main file:
html_template.html:
<li>{{ i.name }}</li>
xml_template.xml:
<loc>{{ i.url }}</loc>
page_loop.html:
{% assign a = "logic I don't want to repeat" %}
{% for i in a %}
{% include {{ include.inner_template }} %}
{% endfor %}
html_main.html:
{% include page_loop.html inner_template="html_template.html" %}
xml_main.xml:
{% include page_loop.html inner_template="xml_template.xml" %}
It would probably be another more elegant (?) solution developing a plugin, but quickly modifying your code, in _includes/page_loop.html:
{% assign a = "something" %}
{% for i in a %}
{%if include.type == "name"%}
<li>{{ i.name }}</li>
{%else if include.type == "url"%}
<loc>{{ i.url }}</loc>
{%endif %}
{% endfor %}
Then each time you include page_loop.html pass an additional parameter specifying which type of output you want:
{% include page_loop.html type="name" %}
or
{% include page_loop.html type="url" %}
Let's say I have a form with a hundred fields. Some of them have description defined:
i.e. Sales=IntegerField('Sales', description='Annual Sales')
some of them do not:
i.e. Name=TextField('Full Name')
in Jinja, how can I check whether description has been set or not?
I've tried
{% for field in form %}
{% if field.description != None %}
<h2>{{field.description}}</h2>
{{field.label}}
{{field}}
{% endif %}
{% endfor %}
I'm trying to iterate through the fields, and create an html header to group the fields into sections.
I was also doing
{%set currDesc="nothing"%}
{%for field in form %}
{% if field.description != currDesc %}
<h2>{{field.description}}</h2>
{% set currDesc= field.description %}
{% endif %}
{% endfor %}
but it ends up creating a ton of <h2>s
upon further research, we can check whether an attribute is defined explicitly in wtforms by using an empty string comparison:
{% if field.description =! '' %}
<h2>{{field.description}}</h2>
{% endif %}
I have the following code working in a Django HTML template. However, it’s quite repetitive. How can this code be simplified?
It translates to, "If you're not staff, you get to see nav.html. If you are staff, you only get to see nav.html if you're on these 4 pages."
{% if not request.user.is_staff %}
{% include ‘nav.html’ %}
{% else %}
{% if request.get_full_path == ‘/one/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/two/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/three/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/four/’ %}
{% include ‘nav.html’ %}
{% endif %}
{% endif %}
From your view, you can send a list containing the paths for which to show nav.html. Something like,
allowed_paths = ['/one/', '/two/', '/three/', '/fourth/']
In your template, you can simply do,
{% if not request.user.is_staff or request.get_full_path in allowed_paths %}
{% include 'nav.html' %}
{% endif %}
I want to loop over all items in a collection that have a certain field set (not empty). I tried this:
{% assign papers_with_demos=site.data.papers | where:"demo", not blank %}
{% if papers_with_demos.size > 0 %}
<h2>demos</h2>
{% for paper in papers_with_demos %}]
...
{% endfor %}
{% endif %}
but it does not work; all papers are returned.
My goal is that the heading "demos" will be shown, only if there is one or more paper with a demo.
If papers are being returned, you must be using data files. If a value is empty for a particular key in a data file it will return false. So you could check the data key with a forloop like:
{% for paper in site.data.papers %}
{% if paper.demo %}
something
{% endif %}
{% endfor %}
You'll find more info in the Data Files section of the Jekyll documentation.
Updated in response to your reply:
{% for item in site.data.items %}
{% if item.random %}
{% assign random-val = item.random %}
{% if random-val %}
<p>true</p>
{% endif %}
{% endif %}
{% endfor %}
This will return true if inside items.yml, you have - random: true, nothing if empty.