Mishaps with .108 .Ordinal feature for image render hooks

The code below creates SEVENTEEN loading=eager, including those coming from the template itself. By the way, it’s not explicited in the version note. In the sourcecode of that page, about 10

{{if .IsBlock}}<figure {{else}}<span{{end}} 
class="image flex flex-col right {{with .Attributes.class}}{{.}}{{end}}"> 
{{ with .Title }}<figcaption>{{.|markdownify}}</figcaption>{{ end }} 
<a href="{{ .Destination | safeURL }}" target="_blank">
<img loading={{ if gt .Ordinal 7 }}"lazy"{{ else }}"eager"{{ end }} 
src="{{ .Destination | safeURL }}.small" alt="{{ default .Title .Text }}"
 {{ with .Title }}title="{{ . }}" {{ end }}/></a>
{{if .IsBlock}}</figure>{{else}}</span>{{end}}

The condition seems to target images at random without rhymn nor reason, I tried switching eager/lazy and .Ordinal/7, or increase/decrease the number, that does change the result somehow but not with any pattern I can recognize.
I tried with that:

<img loading={{ if gt .Ordinal 0 }}

and it scatter lazy and eager absolutely randomly all over the file.
I also noticed that anything inside that shortcode is ignored:

{{ $float := (default "right" (.Get 1)) }}
<aside class="floating_frame" style="
{{with (.Get 0)}}
	border:thin solid {{.}}; color:{{.}};
{{end}}
{{ if (eq $float "right")}}
	float:right;
{{else}}
	float:left;
{{end}}
{{ with (.Get 2)}}
	background-color: {{.}}
{{end}}">
{{ .Inner | markdownify }}
</aside>

Is there a reason ?

Your image render hook (it is not a shortcode) generates invalid HTML. You cannot place a figcaption element inside of anything but a figure element. Please learn to run your rendered HTML through a validator.

When I use your render hook, a markdown file with 10 images is rendered with only 2 images marked as “lazy”, which is the expected result (the .Ordinal begins at zero).

No, it does not.

If you need additional assistance, post a link to the public repository for your project.

1 Like

And now, does it build ?
I should thank you, I could make an extremely instructive sample repository, something I struggled before.
I listened to your advice regards to figcaption too. I just wanted to keep the semantic aspect, and didn’t know it was forbidden inside a … I didn’t know it was a block level element.

Sample repository

I changed to the extended version, which canceled a lot of the weird ßë‘ßë happening, but not for this .Ordinal issue though.
I still see the count for .Ordinal is reset when inside a shortcode, even the markup calls for pictures ![sfds](sfdsf) comes from the file.

The .Ordinal returned by a particular short code counts the number of times that particular shortcode was used on a given page.

The .Ordinal returned by a particular render hook counts the number of times that particular render hook was used on a given page.

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