{% extends 'base.html' %}
{% block content %}
<p>Welcome to home page.</p>
<p>{% user.groups.all() %}</p>
{% endblock %}
At the moment I'm trying to figure out how I could even get all the user's groups to show on the page. This results in an error.... Invalid block tag on line 5: 'user.groups.all()', expected 'endblock'. Did you forget to register or load this tag?
I have tried to do if statements, but it seems break as soon as it meets one condition. For example if user is a part of test1 and test2 groups, I'd like for it to display test1 and test2, but it only displays test1.
{% extends 'base.html' %}
{% block content %}
<p>Welcome to home page.</p>
{% if user.groups.all.0.name == "test1" %}
<p>test1</p>
{% if user.groups.all.0.name == "test2" %}
<p>test2</p>
{% endif %}
{% endblock %}
In the first code, you should have used {{ }} instead. In order to access groups, do this:
{{ user.groups.all }}
and to check for a specific group:
{% if desired_group in user.groups.all %}
some html..
{% endif %}
if you want to output specific html for each group:
{% for group in user.groups.all %}
{% if group == desired_group %}
some html..
{% endif %}
..some more conditions..
{% endfor %}
Related
Hello I am trying to make a post using jeckyll and as part of my post I would like to show some liquid code. The post should display the IF statement as part of the post text (example below).
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
I have tried to post this as
{% highlight liquid %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
and also
{% highlight markdown %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
but anything I try seems to be still executing the liquid code.
Is there a way to display the IF statement my post?
Try wrapping your liquid code in {% raw %} {% endraw %} like this:
{% highlight liquid %}
{% raw %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endraw %}
{% endhighlight %}
The raw tag will disable any liquid processing and output your code as desired.
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" %}
In a page.template, I'm trying to display a separate line for every product I have in the cart, meaning that if the same product is present, in example, 3 times, I need to show it in 3 separate lines.
The basic structure is this:
{% for item in cart.items %}
{% for quantity in item.quantity %}
<p>show something</p>
{% endfor %}
{% endfor %}
But that <p>is not showing.
This sintax is working:
{% for item in cart.items %}
{% for i in (1..item.quantity) %}
<p>show something</p>
{% endfor %}
{% endfor %}
(snap...! auto high-five)
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.
I want to create a little side navigation for on-page navigation.
The site contains multiple images one above the other, the navigation is inside every single image and is linking to the individual id's.
I am using jekyll with the liquid templating engine. To not hard code every single element I created a for-loop which gets its data of a seperat .yml file.
This is how it should looks like:
Image
My problem is that its not working on the first element. Inside the first navigation element the first circle should be selected. But its not:
Image
This is the code:
{% for element in site.data.elements %}
{% capture number %}{{ forloop.length }}{% endcapture %}
<section id="spezial-{{forloop.index}}" {% assign imgIndex = {{forloop.index0}} %} class="spezial-img" style="background-image:url('{{ element.bild }}');">
<div class="container spezial-container">
<div class="sub-navi">
<ul>
{% for y in (1..number) %}
{% if imgIndex == naviIndex %}
<li></i></li>
{% else %}
<li></i></li>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
</section>
{% endfor %}
You're comparing two counter that are not working the same way.
{% assign imgIndex = {{forloop.index0}}
This will count from 0 to array.size-1
{% assign naviIndex = {{forloop.index}}
This will count from 1 to array.size
As your not in the same "time zone", for the first image you have
imgIndex == naviIndex
0 == 1 -> false
And then, starting from the second loop,one time in each loop you will reach an equality but not on the right element.
On the second imgIndex element, for example, the active class will be set on the first naviIndex element but it's wrong. And it's the same for all following elements.
Correct code can be :
{% for element in site.data.elements %}
{% capture number %}{{ forloop.length }}{% endcapture %}
{% assign imgIndex = {{forloop.index}} %}
<section id="spezial-{{imgIndex}}" class="spezial-img" style="background-image:url('{{ element.bild }}');">
<div class="container spezial-container">
<div class="sub-navi">
<ul>
{% for naviIndex in (1..number) %}
{% if imgIndex == naviIndex %}
<li></i></li>
{% else %}
<li></i></li>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
</section>
{% endfor %}