i732
February 9, 2021, 5:25am
1
Pretty sure I’m messing something up, it’s working except for the "last element is using the md name and not the title:
{{ $url := replace .Permalink ( printf "%s" .Site.BaseURL) "/" }}
{{ $title := .Title }}
{{ $length := len (split $url "/") }}
{{ $new := sub $length 1}}
<ul class="uk-breadcrumb">
{{ range $index, $element := split $url "/" }}
//This is the last element, want to display the title
{{ if eq $index $new }}
<li>{{ $title }}</li>
{{ else }}
{{ if not $index}}
<li><a href="/">home</a></li>
{{ end }}
{{ if ne $element "" }}
<li><a href="{{ . | absURL }}">{{ . }}</a></li>
{{ end }}
{{ end }}
While I don’t know about your setup, here’s how I generate my breadcrumb, maybe it’ll help you. Thankfully, even I’m using UIkit.
My structure is made for organization like this: Home > Section > Title
.
<ul class = "uk-breadcrumb uk-link-reset">
<li>
<a href = "/">
Home
</a>
</li>
{{- if .IsPage -}}
{{- with .Section -}}
<li>
<a href = "/{{- . -}}/">
{{- humanize . -}}
</a>
</li>
{{- end -}}
<li>
<span>
{{- .Title -}}
</span>
</li>
{{- end -}}
</ul>
Here’s my entire partial: Portfolio/breadcrumbs.html at v2 · Hrishikesh-K/Portfolio · GitHub
Output looks something like:
Check it here: ITX | Hrishikesh Kokate
I know this doesn’t necessarily answer why your code isn’t working. If I would have understood it, I’d have suggested what you can do. But maybe the above approach works for you?
{{ if ne $element "" }}
<li><a href="{{ . | absURL }}">{{ . }}</a></li>
{{ end }}
in @i732 original code clearly states to use some form of url for the title between the <a>
tags. I would guess that not the last one is wrong, the first two just fit the scheme by accident. You need to do something along the lines of .getPage
for .
and then use .Permalink
and .Title
instead of {{ . }}
.
i732
February 9, 2021, 5:28pm
4
What are you talking about original code?..
My code works, you can literally see it, I’m trying to use title that’s the literal question I’m asking.
Literally: You are splitting the permalink (an URL) and create a breadcrumb with each part of the permalink (an URL).
i732
February 9, 2021, 6:41pm
6
Yes, that is what it does. Congrats!
i732
February 9, 2021, 6:47pm
7
Thank you so much, I realized that “humanize” would work.
<li><a href="{{ . | absURL }}">{{- humanize . -}}</a></li>
If you want the title, use the title My exemple:
The easiest way in my opinion.
a partial breadcrumb.html
{{ with .Parent }}
{{ partial "breadcrumb.html" . }}
<a href="{{ .Permalink }}">{{ if .IsHome }}Home{{ else }}{{ .Title }}{{ end }} </a> ><br>
{{ end }}
The breadcrumb call in your templates (without the page you are on):
{{ partial "breadcrumb" . }}
The breadcrumb call in your templates (with the page you are on):
{{ partial "breadcrumb" . }} <a href="{{ .Permalink }}">{{ .Title }}</a>
i732
February 10, 2021, 7:46pm
9
I do want to use title but the issue is I need to have a reference to the last index.
system
Closed
February 12, 2021, 7:46pm
10
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.