Hugo Shortcode to generate other content in current content page


#1

Hello,

Following my github ticket, I open this discussion around Hugo shortcode feature.

I use a shortcode to render other pages content in a given page, and this can get generated recursively :

{{ $found := where .Site.Pages ".Params.code" "=" .Params.src }}
{{ if len $found | ge 1 }}
		{{ range $found }}
			<div style="border: thin solid black">
				<h4><b>{{ .Title }}</b></h4>
				<p>{{ .Content }}</p>
			</div>
		{{ end }}
	{{ end }}
{{end}}

I use the followign code to use this procedure :

---
code: 'CODE01'
title: 'Some page'
---

{{% rendering src="CODE02" %}}

But Hugo, if run in one pass, is sometimes missing the .Content of the other pages rendered in the current page. I have to force multiple pass to ensure that everything gets generated correctly :

./hugo.exe server -s src/ -d public/ &
hugo_pid=$!
sleep 4
touch $1/layouts/shortcodes/rendering.html
sleep 2
touch $1/layouts/shortcodes/rendering.html
sleep 2
touch $1/layouts/shortcodes/rendering.html
sleep 2
touch $1/layouts/shortcodes/rendering.html
kill $hugo_pid

Shortcodes looks a strong tool, but I feel like this is a strong limitation of shortcodes. Am I using Hugo wrongly ? Should I do it in another way, or with another tool ?

Thanks


#2

In general, .Content is not available in Shortcodes. This is, in general, known as the chicken and the egg problem. There are are open issue(s) to improve on this, but but don’t expect it to be fully fixed, because that is impossible. Think in the terms of the laws of Newton and Einstein and friends. So do this in regular templates.


#3

I don’t understand how I could do this using a regular template.

I understand that it is unfixable in recursive loops cases, but I don’t understand why it would be unfixable in the following case :

Page1
   \- Include content of -> Page2
                                \- Include content of -> Page 3
                                \- Include content of -> Page 4
   \- Include content of -> Page3

Is this case supposed to be handled by the future fixes of the bugs your mentioned ?

Thanks


#4

My main point is that you cannot include yourself. And then there are some issues with concurrency (where content isn’t available in time), but this is not on my “priority list” of things to fix, so I would look for alternatives.