I’m trying to figure out how I can explicitly include a space between the file location url and width size to be used in an image srcset.
In my toml I have
---
srcset: /images/mockups/apple-watch-white-mockup.png 300px, /images/mockups/apple-watch-white-mockup@2x.png 600px
Which is then called by my partial
<img src="{{.src}}" {{if .srcset}}srcset="{{.srcset }}"{{end}} alt="{{.alt}}" {{if .height}}height="{{.height}}"{{end}}{{if .width}}height="{{.width}}"{{end}}>
And outputs
<img src="./images/mockups/apple-watch-white-mockup.png" srcset="/images/mockups/apple-watch-white-mockup.png%20320px,%20/images/mockups/apple-watch-white-mockup@2x.png%20600px" alt="Wearable device mockup." class="loading" data-was-processed="true">
I checked out the following related topics and tried the urlize, safeURL, HTML and markdownify function, but havent had any luck solving my problem.
Hi, I’m trying to get a shortcode to work where it will create a responsive image with the srcset attribute.
So, the shortcode takes in the original image name “hello.jpg” and should output something like:
<img src="hello.jpg" srcset="hello-1080.jpg 1080w, hello-600.jpg 600w, hello-300.jpg 300w" alt=""/>
I would like it to work with other image extensions too, but for now just .jpg is ok.
Here’s what I have so far in image.html shortcode:
{{ $input := index .Params 0 }}
{{ $im := (print $.P…
I’m using the following code to parse the list of tags in the front-matter of my posts and display them as a list of clickable links at the end of each post:
{{ range .Params.tags }}<a class="tag" href="/tags/{{lower . }}">{{lower . }}</a>, {{ end }}
[The {{lower .}} lower cases each tag to avoid any case-sensitivity problems.]
I’ve run into a bit of a problem now, however. If the tag in the front-matter consists of two words [eg. “VW beetle” Hugo will generate the tags category as /pub…
In summary, I’d either like to have a srcset string parameter or maybe something that can be used as a range like…
---
# FIRST CHOICE
srcset: /images/mockups/apple-watch-white-mockup.png 300px, /images/mockups/apple-watch-white-mockup@2x.png 600px
# SECOND CHOICE
srcset:
- 300: /images/mockups/apple-watch-white-mockup.png
- 600: /images/mockups/apple-watch-white-mockup@2x.png
Whichever one is easier or possible.
Thanks!
maiki
November 26, 2017, 5:00am
2
Do you have a site repo or demo repo up somewhere? I’d check it out, but I don’t want to build a demo.
Randy_Lough:
In my toml I have
You examples are YAML.
I built a repo for this: https://github.com/randallmlough/hugo-srcset-help
Note: I use a _index.md file which holds the YAML data ( )
Thanks!
maiki
November 27, 2017, 10:17am
4
There is a quirk in Go templates that doesn’t make total sense to those of us new to them (and I am very new!), but as @moorereason pointed out , this is a question that gets asked a lot.
@Randy_Lough , here is the modified img.html
from your demo repo:
{{ $srcset := .srcset }}
<img class="img-fluid" src="{{.src}}" {{ with .srcset }}{{ printf "srcset=\"%s\"" $srcset | safeHTMLAttr }}{{ end }} alt="{{.alt}}">
There are a lot of moving parts and caveats for why that looks like that, and honestly, I don’t quite recall where I read them all. Eventually we’ll need to explain this better, but for your demo, that outputs:
<img class="img-fluid" src="/images/apple-watch-white-mockup.png" srcset="/images/apple-watch-white-mockup.png 320px, /images/apple-watch-white-mockup@2x.png 600px" alt="A wearable device">
Cameron, the reason I mentioned you is to ask if you could double-check that. I understand what I am doing there, but am not confident enough to call it a day for someone else.
3 Likes
Yes, a use case of safeHTMLAttr
came up very recently .
1 Like
You’re the man, It’s working beautifully! Thank you.
maiki
November 27, 2017, 9:32pm
7
Well, I am certainly a person . Glad it works for you.
1 Like
If you wanted to do this using the second option, as in you were ranging through an array of srcset paths. how would you format that? Can’t seem to figure out where to open the srcset quote.
maiki
December 12, 2017, 6:25pm
10
Please create a new topic with your question. You can reference this one, but you ought to provide the example front matter in the new topic.