Just simply output this base64, how to do it?

{{ if .Params.FeaturedImagePreview }}
    {{ $img := .Params.FeaturedImagePreview }}
    <img class="mw-100 w-100 h-100 rounded-start" src="{{ $img }}" alt="00....">
{{ else }}
    <img class="mw-100 w-100 h-100 rounded-start" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="{{ .Title }}">
{{ end }}

How can we simply output $img as a string output without additional operations?

The functions I have tried are as follows, but none of them work. It came out for a while, and then it disappeared again. I don’t know how to operate it.

base64Encode 
safeHTMLAttr
string
safeCSS
print

I have also read the following posts and examples, and I feel that it is not what I want, that needs to split the base64 code, and it is also related to the format. . .

The current result is this, not the gif transparent image, which proves that $img exists, but something is wrong.

<img class="mw-100 w-100 h-100 rounded-start" src="#ZgotmplZ" alt="00....">

Search the forum for that and use something like safeHTMLAttr.

Like this, I added a lot, but the string is still wrong. . .

<img class="mw-100 w-100 h-100 rounded-start" src="{{ .Params.FeaturedImagePreview | safeHTMLAttr }}" alt="1200....">

This should work:

<img class="mw-100 w-100 h-100 rounded-start" {{ printf "src=%q" .Params.FeaturedImagePreview | safeHTMLAttr }} alt="1200....">

safeHTMLAttr handles the whole attribute, not just the value.

2 Likes

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