Accessing Page.Truncated from a shortcode

Hi,

Is it possible to access Page.Truncated from inside a shortcode?

I’d like to create a shortcode that injects some HTML only when showing a post’s truncated version. But. $.Page.Truncated is always false inside the shortcode. Is it a bug?

No. It has been discussed before. It is the chicken and the egg problem. You cannot know if the page is truncated until you have rendered all the shortcodes.

Then, how to vary the post HTML based on what .html page it’s rendered on?


My real problem: I’m trying to recreate my old Wordpress theme in Hugo. Wordpress inserts the “Continue reading” link inline, exactly where the <!--more--> marker is. Hugo can’t do that, it has to be on a different paragraph. So I figured I could define a shortcode like {{< more >}} defined as:

{{if $.Page.Truncated }}
<a href="{{$.Page.URL}}">{{$.Page.Param "read_more_copy" | default "Continue reading →" }}</a>
{{end}}HUGOMORE42

I’ve also tried using Scratch to send information to the shortcode, e.g. in list.html:

{{ .Scratch.Add "home" 1 }}  

and in the shortcode:

{{if .Page.Scratch.Get "home" }} ... {{end}}

But, again, it is always “false”.

I don’t know if it’s the best way, but the way I figured out is:

  1. Define the shortcode as e.g. MYMOREMARKERHUGOMORE42 (a concatenation of a custom “more” marker to replace later, and the one Hugo uses internally)
  2. In single.html or wherever the article appears in full, remove your marker using {{ replace .Content "MYMOREMARKER" "" | safeHTML }}
  3. In list.html or wherever you want just the summary + “read more” link, use e.g.:
    {{ $more := (print " <a href=\"" .URL "\">" ( .Param "read_more_copy" | default "Continue reading →" ) "</a>") }}
    {{ replace .Summary "MYMOREMARKER" $more | safeHTML }}