Inject HTML into gotmpl Go template #14746
Open
+3
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This injection syntax seems to work, and was suggested here: #13035. I am unsure if there is a better/more modern syntax? Note the method names in yellow at the top are not yellow any more in the HTML, can we re-inject the gotmpl back into the HTML again?
Here are the current results in a
.gotmplHugo Go template:Code example
{{ $image := .Page.Resources.GetMatch (printf "%s" (.Destination | safeURL)) }} {{ $small := $image.Resize "480x webp q90 Lanczos" }} {{ $medium := $image.Resize "960x webp q90 Lanczos" }} {{ $big := $image.Resize "1920x webp q90 Lanczos" }} {{ $alt := .PlainText | safeHTML }} {{ $caption := "" }} {{ with .Title }} {{ $caption = . | safeHTML }} {{ end }} <figure> <a href="{{ $image.RelPermalink }}"> <img sizes="(min-width: 1080px) 960px, 91.05vw" srcset="{{ $small.RelPermalink }} 480w, {{ $medium.RelPermalink }} 960w, {{ $big.RelPermalink }} 1920w" src="{{ $image.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" alt="{{ if $alt }}{{ $alt }}{{ else if $caption }}{{ $caption | markdownify | plainify }}{{ else }} {{ end }}" loading="lazy" decoding="async" > </a> {{ with $caption }} <figcaption>{{ . | markdownify }}</figcaption> {{ end }} </figure>Also, on a side note, there is a much more recent
tree-sitter-go-templateavailable at https://github.com/ngalaiko/tree-sitter-go-template. The version we are currently using is an outdated fork of this repository.