Related to this topic, I would like to pass some values in a partial by defining them via dict like below so that they appear inside the <img> tag. How can this be achieved?
{{- range $k, $v := .Pages | first 2 }}
{{- if not $k }}
{{- partial "home/featured-image.html" <!-- add `fetchpriority="high" here --> }}
{{ else }}
{{- partial "home/featured-image.html" <!-- add `loading="lazy" here --> }}
{{- end }}
execute of template failed at <.FetchPriority>: can’t evaluate field FetchPriority in type *resources.resourceAdapter
I have the image partial (with image processing), which is called inside another partial that is then called in the list page. I am not sure if context is the issue here.
I guess you’re accessing those variables in other context, such as within the with statement. You can either assign it to a local var, or use a leading $ to access the outside context, i.e. $.FetchPriority.
{{/* PUT IT AT VERY TOP OF THE PARTIAL */}}
{{- $lazyLoading := default true .LazyLoading }}
{{- $fetchPriority:= default "" .FetchPriority}}
{{- with RESOURCE }}
<img
src="..."
{{ if $lazyLoading }}loading="lazy"{{ end }}
{{ with $fetchPriority }}fetchpriority="{{ . }}"{{ end }}
/>
{{- end }}
can’t evaluate field LazyLoading in type page.Page render: failed to render pages: can’t evaluate field LazyLoading in type page.Page
I even removed the second partial and added the code directly in the list template but the rror persists. The image processing is wrapped with with ({{ with or (page resource) (fallback image) }}), so I am not sure if that is affecting the context.
Build successfully, are there other places invoked the home/featured-image.html?
It most likely that there are some places you forgot to modify, sth like following.
I modified one of those statement as following, produced same error as you posted.
{{ partial "home/featured-image.html" . }}
execute of template failed at <.LazyLoading>: can’t evaluate field LazyLoading in type page.Page
I couldn’t provide any help without seeing the full source code to reproduce the issue, since the code snippet you provided works as expected. But that’s just a context issue, you can double-check your codes, and make sure those files are modified and saved.