And today I experienced intermittent, unexpected results where the appended page seemed to get stuck, using the same value from page to page. It’s difficult to describe, but easily repeatable.
I saw this when running hugo server and when building the site with hugo.
git clone --single-branch -b hugo-forum-topic-41532 https://github.com/jmooring/hugo-testing hugo-forum-topic-41532
cd hugo-forum-topic-41532
hugo server
I’m trying to understand your question. What does “get stuck” mean?
I’m viewing the page /section-1/section-1-2/section-1-2-1/page-13/ (then click other pages) as example.
Here is what I’m seeing:
{{ $pages := $posts | append $currentPage }} works as expected: $currentPage is “always” appended to the slice (4 links).
{{ $pages := $posts | append (slice $curentPage) }} does the same thing.
{{ $pages := $posts | union (slice $curentPage) }} joins two slices without duplicated items. The expression is same as union (slice $currentPage) $posts. The link of the current page is on top of the page list.
It seems kind of normal. What do you want to achieve?
I want to learn from this example too. Thank you.
Edited:
I’m still guessing but I push a PR anyway.
Both “append” and “union” approaches with slice creates multiple slices. And I personally think that the expression is quite complicating. I try to make it simple using Hugo’s in function:
{{- if not (in $regularPagesInFirstSubsection $currentPage) }}
<h3 style="background: yellow;"><a href="{{ .RelPermalink }}">{{ .Title }}</a> (weight = {{ .Weight }})</h3>
{{- end }}
Then you can {{- range $regularPagesInFirstSubsection }} directly.
When you run the example site, the home page asks you to run the given command repeatedly in your terminal, so that you can easily see the inconsistent results from one build to the next.
Yes. I tried repeating the command a few times. Those two approaches have different results. But the results for each approaches are consistent. (Hugo v0.105.0)
Let me rephrase the question. When you ran the command repeatedly with the unmodified repository, did you see different results from one run to the next?
It is quite strange. I’ve tried > 30 times but the results are consistent. I’m using a basic instance of Lightsail VPS to test your repo. The server is slow but I think it doesn’t matter. Build time is around total in 50 ms.
I can reproduce that issue on my MacBook. Hmm… that’s really weird, as the implementation of append should be straight forward (no magic caching going on). Almost like I suspect there is a issue in the templating (Go stdlib) code.
Btw, you can append single objects to a slice, no need to make a slice.
I have updated the issue above. So, I would call it a Hugo bug, but it should be not be a common thing to experience. I need to think a little about how to fix it, because the current behaviour has its upsides for the majority of the append use cases (it does not allocate memory if the slice has the needed capacity, which is especially great when appending in a loop).