Img inside linke

I am new here. I am having issue with the following:

{{ $img := resources.Get (.Get 0) }}
{{ with $img.Resize (.Get 1) }}
    <a href="{{ .Get 2 }}"><img src="{{ .RelPermalink }}" alt="image-custom-size-02"></a>
{{ end }}

Above is not working. It works if I remove <a href=“{{ .Get 2 }}”.
shortcodes file name is imgcus.html and I do like.
{{< imgcus “images/myimage.jpg” “360x” “/about/” >}}
please help.

And what exactly does “not working” mean? Error message? Image not visible? Wrong image?

1 Like
{{ $img := resources.Get (.Get 0) }}
{{ $url := .Get 2 }}
{{ with $img.Resize (.Get 1) }}
    <a href="{{ $url }}"><img src="{{ .RelPermalink }}" alt="image-custom-size-02"></a>
{{ end }}

You need to learn about the context. Which is a complicated thing. The DOT inside of the with $img.Resize is related to the $img.Resize variable, not the context of the shortcode you are in. So basically .Get 2 at that point asks $img.Resize about their .Get function, which it does not has, which results in the error you are getting. I just think it is, but we don’t know, because you did not post the error you are getting. But it should say something like “Get is not known on $img.Resize object” or something like that.

So what we do is: Move the .Get 2 out of the with and into a variable like I did in the code above.

{{ $img := resources.Get (.Get 0) }}

<-- . (DOT) here means "the shortcode"

{{ $url := .Get 2 }}
{{ with $img.Resize (.Get 1) }}

<-- . (DOT) here means "the value/item/object of whatever $img.Resize does"

    <a href="{{ $url }}"><img src="{{ .RelPermalink }}" alt="image-custom-size-02"></a>
{{ end }}

Other than that: “Not working” is NOT an error message. That long line with the fancy chaos of numbers and filenames on your CLI is the error message :wink: We might have been able to work better with that.

1 Like

Instead of initializing a new variable, access the template context with $ (e.g., $.Get 42).