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/, I get an array of text (no ").

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


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


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:

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.