$ is unexpectedly added at the end of every line

Hello all.

I’m trying to create an easy way to mark pages on my site as deleted, so that webmentions work properly. What I have in mind is having a front matter deleted variable that would make Hugo create a .htaccess file containing Redirect 410 index.html.

I added a custom format to my config.toml:

[mediaTypes]
  [mediaTypes."text/htaccess"]
    suffixes = ["htaccess"]

[outputFormats]
  [outputFormats.htaccess]
    mediaType = "text/htaccess"
    baseName = ""
    isPlainText = false
    notAlternative = true

and added “htaccess” for page to the [outputs] section. Then I created layouts/_default/single.htaccess:

{{ with .Params.deleted }}Redirect 410 index.html{{ end }}

It almost works. .htaccess is created for every page, and for those pages that have deleted = true in the front matter, it contains the text. But…

But every line of the created .htaccess files ends with $ sign. I’ve read the documentation, the issues on GitHub, googled extensively and searched the forum, but I still can’t figure out where these $s come from and how to get rid of them.

I’m sure there’s something obvious I’m missing, but what is it?

It seems like there is a stray dollar sign somewhere in your project’s files.

Use your text editor to search for it.

If that is not the case then you will need to share the project for us to see it in full.

Hugo does not append special characters in generated files.

I figured as much, but where can it be? I mean, isn’t my layouts/_default/single.htaccess supposed to basically start with the clean state (empty file) and then add what I explicitly asked for?

Yes. If that is not case then something else may be going on.

Hence I asked you to share the project if you cannot figure it out on your own.

Or if you cannot share the project then put together a minimal repo with sample content that replicates the issue.

I figured out a workaround, but still don’t understand the underlying mechanics. And judging by the workaround, it looks like somewhere something gets parsed wrong.

The working layouts/_default/single.htaccess I ended up with looks like this:

{{- with .Params.deleted -}}Redirect 410 "{{- $.RelPermalink -}}"{{- printf "\n" -}}
{{- end -}}

So, in essence, “dashing out” all the curly braces removes the $ signs at the end of every line, with the added benefit of not even creating the empty .htaccess where it isn’t needed.

Judging from all I know about Go templates, it shouldn’t work this way.

If the problem is solved by hardcoding a line break then perhaps you should remove some of those dashes within the curly brackets.

See: https://gohugo.io/templates/introduction/#whitespace

However I cannot help further without seeing a project.

1 Like

No, hardcoding a linebreak was just an icing on that cake.

Here. This:

{{ with .Params.deleted }}sometext{{ end }}

generates sometext$ if deleted is set, and just $ when it isn’t

while this:

{{- with .Params.deleted -}}sometext{{- end -}}

generates sometext when deleted is set, and no page at all when it isn’t.

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