Build error when using PrevInSection

Hey guys, I get build errors when I try the code below on my blog (ending tags have been removed to shorten it here). It’s a function that links to the previous post while showing that post header image as a background. It gives errors while running locally but works. Problem is it crashes when being Netlify tries to build it.

<a href="{{if .PrevInSection}}{{.PrevInSection.Permalink}}{{end}}">
    <div id="prefooter" style="{{.PrevInSection.Params.hero}}.jpg');">...

Any clues?

It would be great if you could include the … build error …

Of course

ERROR: 2016/11/22 21:31:21 template.go:132: template: theme/partials/prefooter.html:2:156: executing “theme/partials/prefooter.html” at <.PrevInSection.Param…>: can’t evaluate field Params in type *hugolib.Page in theme/partials/prefooter.html
ERROR: 2016/11/22 21:31:21 template.go:132: template: theme/partials/prefooter.html:2:156: executing “theme/partials/prefooter.html” at <.PrevInSection.Param…>: can’t evaluate field Params in type *hugolib.Page in theme/partials/prefooter.html

OK, so i removed {{.PrevInSection.Params.hero}} and now it works both locally and on Netlify. The sole question remaining: is, and if so, how is it possible to call .PrevInSection hero-image?

/Thanks

@samrich I use similar templating with Hugo 17 on Netlify and don’t have this issue. Also, it looks like your templating sets a conditional for just the anchor link and no conditional for the prefooter image, which probably means the build is hitting a the most “previous” post and then .PrevInSection doesn’t actually have a value.

Try this…

{{with .PrevInSection}}
<a href="{{.Permalink}}">
    <div id="prefooter" style="{{.Params.hero}}.jpg');">
</a>
{{end}} 

Also, I don’t know how you are doing your front matter, but it seems like this would be easier if you are styling the hero image as background-image, since it allows you to use more than one file type for your images and (I’m assuming) makes the front matter shorter:

hero: my-hero-image.jpg

And then…

{{with .PrevInSection}}
<a href="{{.Permalink}}">
    <div id="prefooter" style="background-image: url('{{.Params.hero}}');">
</a>
{{end}} 
1 Like

Thanks @rdwatters ! I will give it a try!
You are correct, and it is a CSS-background, I just shortened the code a bit here

@rdwatters it worked like a charm! Thank you! :grinning:

1 Like