You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I am already working on my second Coltrane project, and I started tinkering with the template tags provided, aiming to use them for links and breadcrumbs.
I found the current ones a little limiting, so I built from them my custom ones.
last_url: It is similar to the last path, but it is a filter. You can also add a number to choose a section of the URL you give it (from /hi/how/are/you/ you can pick 2 and would be 'are'), with default of -1.
url_section : another filter that you can use to gets slice, giving a position when to end [:until] (here 2 could be 'hi/how/are/'.
breadcrumb: This tag renders a breadcrumb if you give it a URL, in this case, the slug that comes with the content. It is extremely useful for content-based sites. Mine is made with Bootstrap, but it could be done neutrally.
custom.py
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
def path_list(url:str)->list:
url = str(url)
broken_url = url.strip()
broken_url = broken_url.split("/")
return broken_url
@register.filter
@stringfilter
def last_url(value:str,place=-1):
if value.endswith("/"):
value = value[:-1]
if value.startswith("/"):
value = value[1:]
value = path_list(value)
value = value[place]
return value
@register.filter
@stringfilter
def url_section(value:str,until=1)->str:
url_parts = path_list(value)
url_selected = url_parts[:until]
url_section = "/"
for v in url_selected:
part = f"{v}/"
url_section += part
return url_section
@register.inclusion_tag("tags/breadcrumb.html")
def breadcrumb(url:str):
path_list_local = path_list(url)
list_length = len(path_list_local)+1
url_list = []
for i in range(0,list_length):
section = url_section(url,i)
url_list.append(section)
return {"url_list":url_list[1:]}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, I am already working on my second Coltrane project, and I started tinkering with the template tags provided, aiming to use them for links and breadcrumbs.
I found the current ones a little limiting, so I built from them my custom ones.
last_url: It is similar to the last path, but it is a filter. You can also add a number to choose a section of the URL you give it (from /hi/how/are/you/ you can pick 2 and would be 'are'), with default of -1.
url_section : another filter that you can use to gets slice, giving a position when to end [:until] (here 2 could be 'hi/how/are/'.
breadcrumb: This tag renders a breadcrumb if you give it a URL, in this case, the slug that comes with the content. It is extremely useful for content-based sites. Mine is made with Bootstrap, but it could be done neutrally.
custom.py
breadcrumb.html
Beta Was this translation helpful? Give feedback.
All reactions