Can someone help me with this breadcrumb

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: image

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 {{ . }}.

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).

Yes, that is what it does. Congrats!

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 :wink: My exemple:

I do want to use title but the issue is I need to have a reference to the last index.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.