Hugo inconsistently setting variables

Hello!

i’m having a hard time figuring out what is causing some inconsistent behavior on my hugo site. I do not have a repository so I’ll try to be as descriptive as possible here.

my website has 3 categories of pages:

Blog
Galleries
Cameras

In the front matter of Galleries and Cameras, I have a custom parameter, “camera”
In the Cameras section, I’m calling a shortcode which will pull camera information out of a YAML file and display it in a list next to some pictures of the specific camera. At the end of the shortcode, I use that camera parameter to range through the galleries and show which were shot with that specific camera.

This functionality works like 10% of the time and I don’t know why.

camera/camera1/index.md

+++
title = "Olympus XA"
date = "2023-08-24T18:49:46.000Z"
draft = false
featured_image = "images/1.jpg"
summary = "the pocket rangefinder that could."
tags = ["camera"]
camera = "olympusxa"
+++

{{< camera_info data="camera1" cameraval="olympusxa" >}}
# Thoughts
Here are some random thoughts and reviews about this camera!

shortcodes/camera_info.html

{{ $caminfo := .Params.data }}
{{ $featimg := .Params.fi }}
{{ $cameraval := page.Param "camera" }}
{{ $cameraval }}
<div class="colrow">

	<div class="column">
  		{{ range .Page.Resources }}
			<img src="{{ .RelPermalink }}">
		{{ end }}
	</div>
	
	<div class="column">
		<ul>
			<h3>Camera Information:</h3>
			{{ $order := slice "maker" "model" "format" "style" "lens" }}
			{{ $camera := index .Site.Data.cameralist.cameras $caminfo }}
				{{ range $order }}
					{{/* print all known fields in the desired order */}}
					{{ $k := . }}
					{{ $v := index $camera $k }}
						{{ if eq $k "maker" }}
							<li class="cameraList"><a href="/categories/{{ $v }}" class="listHead">{{ $k }}: {{ $v }}</a></li>
						{{ else if eq $k "format"}}
							<li class="cameraList"><a href="/categories/{{ $v }}" class="listHead">{{ $k }}: {{ $v }}</a></li>
						{{ else if eq $k "style" }}
							<li class="cameraList"><a href="/categories/{{ $v }}" class="listHead">{{ $k }}: {{ $v }}</a></li>
						{{ else }}
							<li class="cameraList">{{ $k }}: {{ $v }}</li>
						{{ end }}
				{{ end }}
				
			{{ range $k, $v := $camera }}
				{{ if not (in $order $k) }}
						{{/* print remaining fields not contained in the desired order;
						by that, additional fields unknown at the time of writing this
						shortcode are still printed at the end of the list */}}
						<li class="cameraList">{{ $k }}: {{ $v }}</li>
				{{ end }}
			{{ end }}
			
		</ul>
	</div>
</div>

{{ $events := where (where .Site.RegularPages "Section" "galleries") "Params.camera" "eq" $cameraval }}
{{ $events }}
{{ if len $events | eq 0 }}
	<p>I have yet to develop any film from this camera!</p>
{{ else }}
<h3>
Sample Galleries
</h3>
{{ end }}

<p>
<ul>
	{{ range where (where .Site.RegularPages "Section" "galleries") "Params.camera" "eq" $cameraval }}
  		  <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  	{{ end }}
</ul>
</p>

At the beginning of this HTML file you can see that I call $cameraval just to see if its populating and a lot of the time it isn’t. If i delete that line maybe it’ll start working again, if I add it or change something trivial again, maybe it’ll work again. Its extremely inconsistent. I WAS using SCRATCH to grab the front matter parameter and change dto a bespoke variable because I read that if scratch is being used by other partials it coudl cause problems so I just got that out of the way and the behavior is still the same.

Thanks for you help, let me know if you need any other information.

Your third call is using global page function. Which according to the docs is a very bad idea.

Might be your hit

1 Like

@irkode is right. page is referring to the “top level Page”, which I guess is very rarely useful. I added that mostly as a proof of concept and a test that we could pass this context all the way down the calling chain, which we use in other features.

What happens if you change the above to

{{ $cameraval := .Page.Param "camera" }}

1 Like

You both nailed it, thank you so much for your input. SO FAR I have had no inconsistent results and was even able to push this live to my server with everything working, which hadn’t happened before.

I am going to put a sticky note on my desk that says “BEWARE THE CAPITALS. CHECK YOUR VARIABLES.” Appreciate it!

2 Likes

Dont foget the dot :wink:

1 Like

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