Set categories in front matter with split .Dir in archetype

When I use split “/” on .Dir, I get an array of string, but when I do hugo new posts/posts.md, I get an array of text (no ").

How to split .Dir into array of string for hugo new?

Ex:

---
categories: {{split .Dir "/"}}
---

output:

---
categories: [posts]
---

I want:

---
categories: ["posts"]
---
categories: {{ split "foo/bar" "/" | jsonify }}
1 Like

again to slow :zzz: – needed to check up some docs and code

but now I have a question about the correct way how to get the directory :

  • .Dir is not listed in the Documentation of archetypes but .File is
  • the DOT is of type *hugolib.archetypeFileData
    looks different from File (hugolib.fileInfo) type in Hugo?

=> So I would think calling .Dir in an archetype is playing with internals ?

with that my code would be - which will work on both Windows and unix because it normalizes the path separators to slashes.

categories: {{ split (.File.Dir | path.Dir ) "/" | jsonify }}
1 Like

sry to ping,
but I would be interested in the technical background - especially as my post is marked as solution.

These all work:

{{ .Page.File.ContentBaseName }}
{{ .File.ContentBaseName }}
{{ .ContentBaseName }}

The first two are expected to work per:
https://github.com/gohugoio/hugo/blob/74b9b8a2297852f37be55d4e12acb79de492707b/hugolib/content_factory.go#L148-L172

I haven’t investigated why the last one works (and I don’t intend to), but I wouldn’t use it.

mmh ok think I got this, so .Dir is part of the last category… results in more Go learing … thx

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