How to parse path.Split?

I’m trying to knock the end off a Section path.

What I’ve got:

Page(/examples/my-nested-section/my-topic/_index.md)

What I want:

Page(/examples/my-nested-section/my-topic/)

I’ve tried using path.Split but when I try to do:

{{ $subfolder := index (path.Split $CurrentSection) 1 }}

I get:

error calling index: can't index item of type path.DirFile

How can I manipulate the results of .Section or .CurrentSection?

That one?

Great, that’s closer. I now get:

Page(/examples/my-nested-section/my-topic/

Just need to add a closing parenthesis to it.

One of the things causing me confusion is trying to work out the different data types in Hugo. It looks like there are strings, paths and arrays. And slices? And then which functions work with each.

Sorry for the howl of pain. I really appreciate Hugo and the docs and this forum - but I think I need to learn Go before I can work out how to template.

{{ $subfolder := printf "Page(%s)" path.Dir(THEPATH) }} should do the trick. If not, you really need to post some sample code of what you are doing there. Blackbox, you know?

printf makes strings out of anything.

And by the way, it’s not Hugo’s data types, it’s Go’s datatypes. And yes… sometimes I think there is something with autism going on with the golang people, but in the end it all makes sense and is extremely simple. Somehow. We (normal people) just don’t get it.

1 Like

I have updated the docs on path.Split to hopefully make it more clear.

It returns this:

type DirFile struct {
	Dir  string
	File string
}

So you do:

{{ $dirFile := path.Split "a/news.html" }} 
{{ $dir := $dirFile.Dir }}
{{ $file := $dirFile.File }}
2 Likes

Thanks for the steer. That’s handy.

And thanks @bep for updating the docs.

I’m trying to do something which I’ve seen raised on here several times but never quite seen achieved: allowing the user to select different versions of the same topic.

I’ve got a site like this:

+---Content\examples
        \---my-first-topic
            |   _index.md
            |
            +---1.1
            |       index.md
            |
            +---1.2
            |       index.md
            |
            \---my-second-topic
                |   _index.md
                |
                +---1.1
                |       index.md
                |
                \---1.2
                        index.md

When I’m on a page, I’m trying to get an ?array of all the pages - both _index and regular - in the current section.

So if I’m at my-second-topic/_index.md, I want:

my-second-topic/_index.md
my-second-topic/1.1/index.md
my-second-topic/1.2/index.md

I’ve tried {{ $pages := (where .Site.Pages "Section" .Section) }} - that just gives me all the pages in /examples/

while {{ $pages := (where .Site.Pages "CurrentSection" .CurrentSection) }} gives me nothing.

So I think I need to do some path manipulation. Something like "give me all pages which have the path of ( .CurrentSection with the end knocked off ) "

Just wanted to add: thank you. Your info removed the blockage.

1 Like

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