[SOLVED] Accessing Page Variables inside with function

Hey guys :slight_smile:

I’m trying to access a Page variable inside a with function, but it doesn’t work. Is there anything I’m doing wrong?

In my template file I call a partial inside the with function:

{{ with .Params.homepage }}
    <div class="col-md-3">
        {{ partial "faq/item.html" . }}
    </div>
{{ end }}

In the partial I try to access the Page Variable like this

<div class="faq-panel"><img class="img-responsive" src="{{ .image }}">
    <div class="faq-panel-content">
        <h4>{{ .headline }}</h4>
        <p>{{ .excerpt }}</p>
        <a href="{{ .Page.Permalink }}" class="center-block">{{ .link }}</a>
    </div>
</div>

Can anyone help?

A Page variable is never going to be available from a partial unless you pass it along as context. Here you only pass the dot which is .Params.homepage at this point.

You will have to pass a dict as context to your partial containing both the page context and .Params.homepage

{{ partial "faq/item" (dict "page" . "homepage" .Params.homepage) }}

Then from your partial

<a href="{{ .page.Permalink }}" class="center-block">{{ .homepage.link }}</a>
1 Like

Thank you very much @regis! That works :slight_smile:

1 Like

i need to solve something related but i’m not able to understand what is wrong.

i’m creating a carousel of images.

in the content .md i set this parameter

images: ["/img/img1.jpg",""/img/img2.jpg]

then in the single.html i do

	{{ if isset .Params "images" }}
		{{ partial "image-gallery.html" (dict . .Site.BaseURL) }}
	{{ end }}

then image-gallery.html is

<amp-carousel width="400"
  height="300"
  layout="responsive"
  type="slides">
  {{range .Params.images}}
	<amp-img src="{{ .Site.BaseURL }}{{ . }}"
    height="300"
    layout="responsive"
    alt="a sample image"></amp-img>
  {{end}}
</amp-carousel>

but compiling i obtain

Building sites … ERROR 2018/08/25 15:32:13 Error while rendering “page” in “post\”: template: C:\src\proviamo\themes\hugo-lamp\layouts\post\single.html:12:35: executing “main” at <dict . .Site.BaseURL>: error calling dict: dictionary keys must be strings
Total in 381 ms
Error: Error building site: logged 1 error(s)

my original code was

	{{ if isset .Params "images" }}
		{{ partial "image-gallery.html" .}}
	{{ end }}

with the same image-gallery.html but compiling i obtain

C:\src\proviamo>hugo server --disableFastRender
Building sites … ERROR 2018/08/25 15:35:57 Error while rendering “page” in “post\”: template: C:\src\proviamo\themes\hugo-lamp\layouts\post\single.html:12:5: executing “main” at <partial "image-galle…>: error calling partial: template: theme/partials/image-gallery.html:9:23: executing “theme/partials/image-gallery.html” at <.Site.BaseURL>: can’t evaluate field Site in type string
Total in 377 ms
Error: Error building site: logged 1 error(s)

can you help me please?thanks