Skip to content

Commit

Permalink
✨ allow breadcrumbs to be dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
krmax44 committed Nov 20, 2023
1 parent a6054d8 commit 7915410
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
27 changes: 25 additions & 2 deletions froide/helper/templatetags/breadcrumb_helper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
from django import template
from django.urls import NoReverseMatch, reverse

register = template.Library()


def normalize_breadcrumb(breadcrumb):
if type(breadcrumb) == tuple:
if type(breadcrumb[1]) == str:
try:
breadcrumb = (breadcrumb[0], reverse(breadcrumb[1]))
except NoReverseMatch:
pass

return breadcrumb
else:
return (breadcrumb, None)


@register.simple_tag()
def get_breadcrumbs(view=None):
if hasattr(view, "get_breadcrumbs") and callable(view.get_breadcrumbs):
return view.get_breadcrumbs()

if hasattr(view, "breadcrumbs"):
return map(normalize_breadcrumb, view.breadcrumbs)


@register.filter
def is_tuple(value):
return type(value) == tuple
def has_link(value):
return type(value) == tuple and len(value) == 2
28 changes: 15 additions & 13 deletions froide/templates/snippets/breadcrumbs.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{% load breadcrumb_helper %}

{% if view.breadcrumbs != None %}
<nav class="container-md" aria-label="breadcrumb">
<ol class="breadcrumb">
{% for breadcrumb in view.breadcrumbs %}
<li class="breadcrumb-item">
{% if breadcrumb|is_tuple %}
<a href="{% url breadcrumb.1 %}">{{ breadcrumb.0 }}</a>
{% else %}
{{ breadcrumb }}
{% endif %}
</li>
{% endfor %}
{% get_breadcrumbs view as breadcrumbs %}
{% if breadcrumbs != None %}
<nav class="container-md" aria-label="breadcrumb">
<ol class="breadcrumb">
{% for breadcrumb in breadcrumbs %}
<li class="breadcrumb-item{% if forloop.last %} active{% endif %}"
{% if forloop.last %}aria-current="page"{% endif %}>
{% if breadcrumb|has_link %}
<a href="{{ breadcrumb.1 }}">{{ breadcrumb.0 }}</a>
{% else %}
{{ breadcrumb }}
{% endif %}
</li>
{% endfor %}
</ol>
</nav>
{% endif %}
{% endif %}

0 comments on commit 7915410

Please sign in to comment.