Weird "{{__hugo_ctx pid=34}}" inside .RenderShortcodes

Coming from Render not triggered from modifying file pulled in by `readFile` · Issue #13823 · gohugoio/hugo · GitHub, I added an “include” shortcode:

{{ with .Get 0 }}
  {{ with $.Page.GetPage . }}
    {{- .RenderShortcodes }}
  {{ else with $.Page.Resources.Get . }}
    {{- .RenderShortcodes }}
  {{ else with resources.Get . }}
    {{- .Content }}
  {{ else }}
    {{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
  {{ end }}
{{ else }}
  {{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
{{ end }}

Weirdly, inside the .RenderShortcode in the second branch (first else if), it’s adding a pre > code element with the text {{__hugo_ctx pid=30}} (the pid changes randomly).

I couldn’t figure out how to reproduce this in the hugo-testing (based on branch hugo-github-issue-13823) but it’s happening on my website and this is all I changed:

Is this a Hugo bug?

When you copied the “include.html” shortcode from the “hugo-github-issue-13823” branch of the github.com/jmooring/hugo-testing repository you changed the indentation:

diff --git a/layouts/_shortcodes/include.html b/layouts/_shortcodes/include.html
index 335fe4dfb..773486ba2 100644
--- a/layouts/_shortcodes/include.html
+++ b/layouts/_shortcodes/include.html
@@ -1,13 +1,13 @@
 {{ with .Get 0 }}
-  {{ with $.Page.GetPage . }}
-    {{- .RenderShortcodes }}
-  {{ else with $.Page.Resources.Get . }}
-    {{- .RenderShortcodes }}
-  {{ else with resources.Get . }}
-    {{- .Content }}
-  {{ else }}
-    {{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
-  {{ end }}
+    {{ with $.Page.GetPage . }}
+        {{- .RenderShortcodes }}
+    {{ else with $.Page.Resources.Get . }}
+        {{- .RenderShortcodes }}
+    {{ else with resources.Get . }}
+        {{- .Content }}
+    {{ else }}
+        {{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
+    {{ end }}
 {{ else }}
-  {{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
+    {{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
 {{ end }}

And since you are (correctly) calling the shortcode using the {{% %}} notation, anything indented by 4 or more spaces is (correctly) interpreted as an indented code block which triggers #12457.

You could either revert the indentation change, or do something like this:

diff --git a/layouts/_shortcodes/include.html b/layouts/_shortcodes/include.html
index 335fe4dfb..2555283a6 100644
--- a/layouts/_shortcodes/include.html
+++ b/layouts/_shortcodes/include.html
@@ -1,13 +1,13 @@
 {{ with .Get 0 }}
-  {{ with $.Page.GetPage . }}
-    {{- .RenderShortcodes }}
-  {{ else with $.Page.Resources.Get . }}
-    {{- .RenderShortcodes }}
-  {{ else with resources.Get . }}
-    {{- .Content }}
-  {{ else }}
-    {{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
-  {{ end }}
+    {{ with $.Page.GetPage . }}
+{{ .RenderShortcodes }}{{/* do not indent */}}
+    {{ else with $.Page.Resources.Get . }}
+{{ .RenderShortcodes }}{{/* do not indent */}}
+    {{ else with resources.Get . }}
+{{ .Content }}{{/* do not indent */}}
+    {{ else }}
+        {{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
+    {{ end }}
 {{ else }}
-  {{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
+    {{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
 {{ end }}

The “do not indent” comments are (obviously) not required, but instead serve as a reminder to not touch this stuff when editing the file in the future.

It’s hard to suppress indentation of one line (vscode/prettier/go-template-PLUGIN) so I played a little..

  • turned all tags to {{- and -}]}
  • and to visualize added some more levels of indentation

→ seems to work fine

did I miss something?

{{/* https://github.com/gohugoio/hugo/issues/13823#issuecomment-3015270202 */ -}}
{{- with .Get 0 -}}
   {{- with . -}} {{/* just to indent a little more */}}
      {{- with . -}} {{/* just to indent a little more */}}
         {{- with . -}} {{/* just to indent a little more */}}
            {{- with $.Page.GetPage . -}}
               {{- .RenderShortcodes -}}
            {{- else with $.Page.Resources.Get . -}}
               {{- .RenderShortcodes -}}
            {{- else with resources.Get . -}}
               {{- .Content -}}
            {{- else -}}
               {{- errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position -}}
            {{- end -}}
         {{- else -}}
            {{- errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position -}}
         {{- end -}}
      {{- end -}}
   {{- end -}}
{{- end -}}

Also seems to fix the original issue if I do like @irkode says, but instead of putting dashes on everything, I just put it on the ending }} so it eats the newlines and leading whitespace only once (I believe).


I’m still a little confused on how .RenderShortcodes differs from .Content. Based on the docs, it sounds like if I switch to .Content, then headings from within the snippet shouldn’t appear in the table of contents. But nothing changes if I swap it. The only thing that seems to affect whether the headings are properly reprocessed to be added to TOC is if I use {{< instead of {{%.

I thought about this because changing .RenderShortcodes to .Content inside the include code also gets rid of the __hugo_ctx but it sounds like that is supposed to be unidomatic and could break different things.

There are a few ways to handle it. Unless the template is rendering an inline element, I prefer to use white space removal only when necessary. And for me, de-indenting with a comment is a reminder when I look at the template again 3 years from now.

1 Like

Your test was invalid.

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