Help debugging resource usage

I need to get an image within a Page bundle from a template (index.html) and I am having a hard time debugging the situation, could someone check if my reasoning is correct ?

In index.html
{{- $image := (.Site.GetPage "/blog/interview-john").Resources.GetMatch "john-avatar.jpg" -}}
{{- $image := $image.Resize "x100 q75 jpg" -}}

This give me an error
``` Failed to render pages: render of "home" failed: "/Users/matthieu/apps/www.pilot.pm/layouts/index.html:198:56": execute of template failed: template: index.html:198:56: executing "main" at <$image.Resize>: nil pointer evaluating resource.Resource.Resize

So to debug I set this
{{ printf "%#v" $image }}

Which gave me this inforamtion, which is an adress to a resource (am I right ?)
&resources.resourceAdapter .... .....

Now that I am sure $image is a resource I should be able to call .resize isn’t it ?

What am I missing ?

Thanks

You need to first use .GetPage to get the relevant Page, then within its context fetch its .Resources like so:

You are perfectly right, I though i was okay with my (.Site.GetPage "/blog/interview-john").Resources [..]

Thanks you

Apart from that, it’s wrong (people with deeper knowledge correct me if I am wrong) to “assign” a variable twice. You use := to create a variable and then must use = to change it’s value/content.

@mhby
The above construct does not work because although you are using .GetPage to fetch that Page, you are not getting its resources because you are not within the Page context.

@davidsneighbour
Yes, you have a point about variable overwrites. It is always better to overwrite than re-assign the same variable However as far as I know re-assigning with := instead of overwriting with = shouldn’t cause a problem (unless we are talking about some huge project -then I don’t know-).

@alexandros
Thanks for the precision, it makes sense now.

@davidsneighbour
You are 100% right and within pure Go it should have throw en error (I make that mistake all the time)

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