Django dropdown divider - html

I have a dropdown menu that i generated by looping through a list of objects. I have a divider after object 0, 10, and 13. However some user don't have access to all objects so the dividers dont show. Can someone suggest a good way have after the last object in each bucket. I.e. bucket [0], [1-10], [11-13]. They buckets are areas in different states.
I'm not necessarily asking for someone to write the code for me. I'm just asking how to do this conceptually as I'm quite new to coding and Django. Any help is much appreciated!
<ul class="dropdown-menu">
{% for i in area_list %}
{% if not i.area_id == area.area_id %}
{% if i.area_id == 0 or i.area_id == 10 or i.area_id == 13 %}
<li>{{ i.area_name }}</li>
<li class="divider"></li>
{% else %}
<li>{{ i.area_name }}</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>

See forloop.last. Try:
context['zones'] = (area_list[:1], area_list[1:11], area_list[11:])
and:
<ul class="dropdown-menu">
{% for areas in zones %}
{% for i in areas %}
{% if not i.area_id == area.area_id %}
<li>{{ i.area_name }}</li>
{% endif %}
{% endfor %}
{% if not forloop.last %}
<li class="divider"></li>
{% endif %}
{% endfor %}
</ul>
You can also prepare the zones and filter empty zones in your view.

Related

I Need Support Code To Link Articles Via Tag

I have a piece of code:
{% if article.tags.size > 0 %}
<div class="related-posts">
<h3>Read More:</h3>
<ul>
{% for tag in article.tags %}
{% for related_post in blogs[blog.handle].articles %}
{% if related_post.tags contains tag and related_post.handle != article.handle %}
<li>{{ related_post.title }}</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
This code has the effect of creating internal links between articles with the same tag as read more . I have a problem. If I put this code on the website it worked. But it only retrieves articles with the same post category. As for the articles that are not in the same category of articles, it will not be retrieved. I changed the code blogs[blog.handle].articles to blog.articles. But it still doesn't work. I use Sense 3.0.0 theme Shopify.
Can someone help me with this problem.
Thank you!
{% if article.tags.size > 0 %}
<div class="related-posts">
<h3>Read More:</h3>
<ul>
{% for tag in article.tags %}
{% for related_post in blogs[blog.handle].articles %}
{% if related_post.tags contains tag and related_post.handle != article.handle %}
<li>{{ related_post.title }}</li>
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}

Django template: Show a link only once if a user belongs in more than one groups

I have a template in django that shows a link if a user has group name different than patient.
My code is:
{% if user.is_authenticated %}
{% for group in user.groups.all %}
{% if group.name != 'Patient' %}
<li {% if request.get_full_path == modules_url %} class="active" {% else%} class="inactive"{% endif %}>
<b style="color:#ff9904;">Modules</b>
</li>
{% endif %}
{% endfor %}
{% endif %}
In case a user is a member of four different groups, the link Modules is shown 4 times in the template.
Is there any way to show it only once?
I think by handle using templatetags should easier.
from django import template
from django.contrib.auth.models import Group
register = template.Library()
#register.filter(name='has_group')
def has_group(user, group_name):
"""
{% if request.user|has_group:'grouName' %}
{# do_stuff #}
{% endif %}
return Boolean (True/False)
"""
group = Group.objects.get(name=group_name)
return group in user.groups.all()
Basicly you can also do with this to get first group:
{% if user.groups.all.0 == 'Patient' %}
...
{% endif %}
I found the solution thanks to here. I used the forloop.counter variable.
{% if user.is_authenticated %}
{% for group in user.groups.all %}
{% if group.name != 'Patient' %}
{% if forloop.counter < 2 %}
<li {% if request.get_full_path == modules_url %} class="active" {% else%} class="inactive"{% endif %}>
<b style="color:#ff9904;">Modules</b>
</li>
{% endif %}
{% endif %}
{% endfor %}
{% endif %}

jekyll page.url doesn't work as it should be

Lets say it's the code snippet:
<ul>
{% assign navigation = site.data.nav %}
{% for link in navigation %}
{% assign active = nil %}
{% for sublink in link.submenu %}
{% if page.url contains sublink.url %}
{% assign active = 'active' %}
{% endif %}
{% endfor %}
{% if page.url contains link.url %}
{% assign active = 'active' %}
{% endif %}
<li class="{{ active }}>
{{ link.text }}
{% if link.submenu %}
<ul>
{% for sublink in link.submenu %}
<li {% if page.url contains sublink.url %}class="active"{% endif %}>
{{ sublink.text }}
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
The nav.yml file looks like this:
- text: First Menu
url: /modules/some text/introduction/
- text: Second Menu
url: /modules/some more text/intro-text/
submenu:
- text: Level Two First
url: /modules/level two first/intro-text/
- text: Level Two Second
url: /modules/level two second/intro-text/
So, every menu item renders and also works fine as it should be. But the only issue i'm getting here is the variable active isn't getting updated according to those if rather staying nil. Also the following line isn't getting that active class rendered even if the page.url contains sublink.url or link.url
<li {% if page.url contains sublink.url %}class="active"{% endif %}>
What might be the issue??

Shopify liquid get related blog posts

In shopify I am using liquid templating to get blog posts which are related to products by their tags, like so:
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><p>{{ article.title }}</p></li>
{% endif %}
{% endfor %}
</ul>
However, if there are no related posts, I would like to display a message such as "No related posts!"
My question is how would I do that? I have contemplated trying to get the articles into an array and testing if it is empty, but I am having difficulty finding out how this is done.
Thanks!
Try something like this:
{% assign related_posts = "" %}
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
{% capture post %}
<li><p>{{ article.title }}</p></li>
{% endcapture %}
{% assign related_posts = related_posts | append:post %}
{% endif %}
{% endfor %}
{% if related_posts.size > 0 %}
<ul> {{ related_posts }} </ul>
{% else %}
No related posts!
{% endif %}
<ul>
{% for article in blogs.blog.articles %}
{% if article.tags contains product.handle %}
<li><p>{{ article.title }}</p></li>
{% else %}
<li>No related blog posts!</li>
{% endif %}
{% endfor %}
</ul>

Jekyll, Liquid - Get all pages from category from page

I have a question in Jekyll Liquid.
I have layout, where I want to show pages from category. To show category I use page.categories variable. When I show in bracket {{page.categories}} is correct.
but I don't know, how to pass to loop?
{% for post in site.categories[page.categories] %}
<li>{{ post.title }}</li>
{% endfor %}
{% for post in site.categories[{{page.categories}}] %}
<li>{{ post.title }}</li>
{% endfor %}
Don't work.
If I passed explicite:
{% for post in site.categories['cat1'] %}
<li>{{ post.title }}</li>
{% endfor %}
It works.
I found another topic:
Jekyll site.categories.{{variable}}?
But it doesn't work.
page.categories is a list (see Page Variables), so you need to loop through it first and pass each category to the loop from your question:
{% for cat in page.categories %}
<h1>{{ cat }}</h1>
<ul>
{% for post in site.categories[cat] %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% endfor %}
This will first display all posts for the page's first category in descending order, then all posts for the page's second category in descending order, and so on.
Thank you. It's work.
Also I can use this code (use first on element of array, because in my case I have only one category per page):
{% assign pcat = page.categories %}
<ul>
{% for post in site.categories[pcat.first] %}
<li {% if post.url == page.url %}class="active"{% endif %}>{{ post.title }}</li>
{% endfor %}
</ul>