Skip to content

Conversation

@David-Else
Copy link
Contributor

@David-Else David-Else commented Nov 6, 2025

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 .gotmpl Hugo Go template:

image
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 }}&nbsp;{{ 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-template available at https://github.com/ngalaiko/tree-sitter-go-template. The version we are currently using is an outdated fork of this repository.

@David-Else David-Else changed the title Inject HTML into gotmpl Inject HTML into gotmpl Go template Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant