Dashes in data folder structure

I am having compilation of my site failing with the following error:

Hugo Error

ERROR 2020/12/03 20:48:10 Process: loading templates: “/home/patrick/Projects/Hugo/dnb-hugo/search-algolia/layouts/_internal/algolia-setup.html:1:1”: parse failed: template: _internal/algolia-setup.html:1: bad character U+002D ‘-’

The layout in question has the following line in line 1:

{{- with $.Site.Data.dnb.search-algolia.configuration.appId -}}

When I range through $.Site.Data.dnb and then call later on deeper .configuration and so on no error comes up. But having the dashed directory name directly in my $.Site.Data call leads to error.

Is this a bug or one of these things that “just” don’t work due to the way upstream libraries work? Maybe there is a way to “mask” the dash, like the backslash in regex formats masks characters with “features”?

This is not just a folder-name issue, TOML too can contain keys with dashes. So if my key with a dash would be inside of a config file as opposed to a folder structure I would expect a way to deal with dashes somehow:

The Go-Docs are strangely silent about variable name conventions. I found a howto about how to use a dashed variable in a gotemplate, but that would require the dashed key being the last one or lead to probably more loops inside loops.

You can use the index function. In your case, it would be {{- with (index $.Site.Data.dnb "search-algolia").configuration.appId -}}.

1 Like

That’s great, I didn’t know that I can “reuse” the index-result this way and would have done some form of with inside of a with. It’s working.

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