Hi,
I can’t do write what I want because of those security checks with html attributes, which I can’t deactivate. I tried with backsticks, double or triple ", I can’t make it work.
This is the code:
I have no clue what printf is doing. I find impossible to escape characters the simple way. There should be a way to disable thock safety checks at least locally that probably more than one user don’t need or ever require, to put it politely.
so you see the first $thumbnail.RelPermalink outputs well, but not the second: /images/article/levi_strauss_1_huf86a05b1ec394239801fbda2a0b2250f_1492618_1000x0_resize_q5_h2_mitchellnetravali_2.webp doesn’t exist, but "/Images"etc does. Why does it change the case ?
Then, background:url( /images/article/levi_strauss_1_huf86a05b1ec394239801fbda2a0b2250f_1492618_1000x0_resize_q5_h2_mitchellnetravali_2.webp") is wrong, the content nof url has one ending double-quote but not an opening one. An issue I did see beforen and couldn’t solve.
I think you mean the way you are used to. Go uses quoting rules that produce the correct result, if you follow the rules. ‘Simple’ quoting (by which I presume you mean old school \" and friends can get pretty hairy, pretty quickly, with certain input and output requirements. (Have you ever tried to do something like the above in C?
Anyway, I think you need to rethink your approach. Think of how you would do the above in a traditional compiled language rather than scripting languages like Bash or Perl. Bash and Perl make certain use cases easy, but have warts of their own, which is why new languages were created (not just because of NIH syndrome). This forum isn’t really the place for griping about how languages work or don’t work the way one expects, it’s a place to learn how to achieve what you want with Go templates, because one wants to achieve certain things with Hugo, which bases it’s templates on Go templates.
I don’t know about C, I only know Ada/Pascal, regexp, a bit of sed bash and fish. I get it, but I hope I don’ have to learn the whole of Go to just escape double-quotes, do I ? I’ll try and digest fmt package - fmt - Go Packages.
Should be "center"}}70{{ which might have been part of your issue.
Splitting this into variables and combing the variables into the final string would, I think, make it easier to detect and solve that kind of syntax error.
If even yourself cannot make sense of tihs without bringing a whole school manual, how are supposed to do casual users of hugo, which just want to escape a double-quote ? Any idea why the case changed for Image ?
try (I’m assuming the image you want is in $thumbnail as a resource, based on your code).
I’m also removing the .Attributes references because they won’t work unless you are in a render-image hook.
{{/* Assumes $centeredImage is set to "center" for centered image; where this comes from depends on where this code lives */}}
{{ $backgroundStyle := printf `style="background:url(%s); background-size:cover; background-repeat:no-repeat;"` $thumbnail.RelPermalink }}
{{ $srcSet := printf "%s %dw" $smallest.RelPermalink $smallest.Width
{{ range uniq (slice $very_small $small $medium $large) }}
{{ $srcSet = $srcSet | append (printf ", %s %dw" .RelPermalink .Width) }}
{{ end }}
<img src="{{ $thumbnail.RelPermalink}}" srcSet="{{ $srcSet }}"
loading="{{with .Page.Param "loading"}}{{ . }}{{else}}lazy{{end}}" {{if or .Title .Text}}alt="{{ default .Title .Text }}"{{end}} {{ with .Title }}title="{{ .|plainify }}"{{end }} sizes="(max-width: 960px) 100vw, (max-width: 604px) {{if eq $centeredImage "center"}}1OO{{else}}50{{end}}vw, {{if eq $centeredImage "center"}}70{{else}}35{{end}}vw"
width="{{ $thumbnail. Width }}" height="{{ $thumbnail. Height }}"
{{safeHTMLAttr $backgroundUrl }}>
Code is untested but should work with at most minor tweaks.