How to get the last element in a path?

Based on this topic which I found after several hours of searching the forum…I tried to use the code in a template but I get this warning below.

.Path when the page is backed by a file is deprecated and will be removed in a future release. We plan to use Path for a canonical source path and you probably want to check the source is a file. To get the current behaviour, you can use a construct similar to the one below:

  {{ $path := "" }}
  {{ with .File }}
        {{ $path = .Path }}
  {{ else }}
        {{ $path = .Path }}
  {{ end }}

Here is my content structure

├───content
│   ├───blog
│   │   ├───cats
│   │   ├───dogs
│   │   ├───pigs
│   │   ├───ants
│   │   ├───bananas
│   │   └───minions
│   └───softs

I use the same code {{ path.Base (path.Split .Path).Dir }} in the default archetype to pull the directory names under blog when using hugo new and this works perfectly. But the error above occurred when I used it in a partial. I want to get the name of the nested section and then urlize it. Is there a simpler way to do this with an example (.CurrentSection also works, but it requires passing context which I am trying to avoid)?

Solved as follows

{{ $path := "" }}
  {{ with .File }}
        {{ $path = .Path }}
  {{ else }}
        {{ $path = .Path }}
  {{ end }}

{{ path.Base (path.Split $path).Dir }}

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