.Site.GetPage - how do I pass a string as an argument?

Hi,

Update - Solved

The following works:

{{ $teaser := string .Params.teaser_image }}
{{ .Site.GetPage "page" "photos/abandoned" $teaser }}

TDLR

How do I pass a string as an argument when it comes to using .Site.GetPage? I am using Hugo v 0.22.

For reference, the repository with all the code for the project is here.

I want to be able to fetch a page but want to pass in a string as an argument. This works sometimes and has very unpredictable results.

This is what I am doing inside my single.html template for the album content type.

{{ $teaser := .Params.teaser_image }}

{{ .Site.GetPage "page" "photos/abandoned" $teaser }}

This works but it raises the following error:

Error while rendering "page": template: albums/single.html:15:46: executing "albums/single.html" at <$teaser>: invalid value; expected string

If I try the following:

{{ $teaser := .Params.teaser_image }}
{{ .Site.GetPage "page" "photos/abandoned" "$teaser" }}	

I get <nil> output as a result.

I don’t understand why this error is popping up. From my understanding, the $teaser variable is a string.

If I do the following, then things work as expected and no error message appears.

{{ .Site.GetPage "page" "photos/abandoned" "cite-foche-reflections.md" }}

For reference, the content file for the album I am rendering is as follows:

---
date: 2017-05-31T13:45:38Z
description: "Abandoned"
identifier: "abandoned"
title: "Abandoned"
weight: "9"
teaser_image: "cite-foche-reflections.md"
---

# Abandoned
This is the content for Abandoned

The template file layouts/albums/single.html looks like this:

<!DOCTYPE html>
<html lang="{{ .Site.LanguageCode }}">
	<head>
		{{ partial "head" . }}
	</head>
	<body>
		{{ partial "header" . }}
		{{ partial "nav" . }}
		<main class="page albums" role="main">

			{{ $teaser := .Params.teaser_image }}

			{{ .Site.GetPage "page" "photos/abandoned" $teaser }}

			<section class="teaser">
				<!-- <img data-src="https://res.cloudinary.com/matt-finucane-portfolio/image/upload/w_2560/v1497000103/abandoned/cite-foche-reflections.jpg"> -->
			</section>
			<section class="content">
				{{ .Content }}
			</section>
		</main>
		{{ partial "footer" . }}
		{{ partial "scripts" }}
	</body>
</html>

Is what I am trying to achieve possible in Hugo, or am I going down a rabbit hole?

Thanks,

Matt

I’m suspecting $teaser is not set for a given page, I suggest something ala

{{ with .Params.teaser_image }}
{{ $.Site.GetPage "page" "photos/abandoned" . }}
{{ end }}
1 Like

Thanks. I will try that too.

I’m also having a similar issue with trying to pass a custom page-level params as an argument to .Site.GetPage

I have a page-level param called “pagelink” set as follows in my front matter:

+++
title = “My Page Title”
pagelink = “custom-section-name”
+++

In my template I would like to pass the pagelink to the .Site.GetPage function.

This does NOT work:

 {{ $sectionName := string .Params.pagelink }}

	{{ with $.Site.GetPage "section" $sectionName }}
		<h3>{{ .Title }}</h3>
	{{ end }}

This does work (by manually setting the variable to “custom-section-name”)

 {{ $sectionName := "custom-section-name" }}

	{{ with $.Site.GetPage "section" $sectionName }}
		<h3>{{ .Title }}</h3>
	{{ end }}

I’ve also experienced issues when trying to compare page-level params in simple logic statements (like comparing two values). I have tried converting them to strings using the string function, but still nothing.

Is there another way to convert page-level params so they can be properly used in other functions and logic statements? Am I missing something?

Not sure about your source code, but what if you replace $sectionName with just .Section in your first example?

And are all the pages that you’re passing in as params actually section pages?

Thanks @rdwatters I forgot to follow up on this one. I managed to work out the issue, maybe it’s a bug. The page I was calling it from (nameAAA.md) was the same name as the section I was trying to call (/nameAAA). All I had to do was rename the page name to (nameAAA1.md) and it worked!

I’ve not yet tested with the recent updates to Site.GetPage in Hugo 0.45, but I think it’s quite a rare use case.