How to extract information from a data file using .Param?


#1

i have a bunch of data files that i use to create lists: https://gitlab.com/fustkilas/oovoo

now, in certain section pages, such as: https://gitlab.com/fustkilas/oovoo/blob/master/content/shows/donut/_index.md

i want to extract information that is in the associated data file, as specified in the dataFile Param in the front matter

how can i do this?


#2

Something like:

{{ $dataFile := "" }}
{{ with .Params.dataFile }}
  {{ $dataFile = index $.Site.Data . }}
{{ end }}
<!-- do something with $dataFile here -->

If the data file is in a subfolder, you’ll need to update the above path to reflect that.


#3

if i do this:

	{{ $dataFile := "" }}
	{{ with .Params.dataFile }}
  		{{ $dataFile = index .Site.Data.shows . }}
	{{ end }}
		
	<h1>{{ $dataFile.title }}</h1>

then i get an error:

Failed to render "/shows/donut/":

Failed to render pages: render of "section" failed: execute of template failed: template: shows/list.html:8:30: executing "shows/list.html" at <.Site.Data.shows>: can't evaluate field Site in type string

	{{ $dataFile := "" }}
	{{ with .Params.dataFile }}
  		{{ $dataFile = index .Site.Data.shows . }}
	{{ end }}

#4

Maybe a context issue. Try it like this instead:

$.Site.Data.shows

#5

ah, how simple, thanks @zwbetz


#6

when i push this to netlify i get the error:

5:33:48 PM: Error: Error building site: failed to render pages: render of "section" failed: "/opt/build/repo/layouts/shows/list.html:11:13": execute of template failed: template: shows/list.html:11:13: executing "shows/list.html" at <$show.title>: can't evaluate field title in type string

why is this? it builds locally but not on netlify?

i am using netlify.toml: https://github.com/gohugoio/hugoDocs/blob/master/netlify.toml


#7

Not sure. Would need to see your actual code and netlify config file to help out.

Per the error message, it has something to do with $show.title


#8

actually, it is failing locally as well, using the same code @zwbetz suggested, and i changed it to $.Site.Data.shows as well

code here: https://gitlab.com/fustkilas/oovoo/blob/master/layouts/shows/list.html


#9

Try putting all the data file logic inside the with

{{ with .Params.dataFile }}
  {{ $show := index $.Site.Data.shows . }}
  <h1>{{ $show.title }}</h1>
  <ul class="show">
    <li>{{ $show.venue }} {{ if $show.external}}<a href="{{ $show.external }}"> → </a>{{ end }}</li>
    {{ if $show.curator }}<li>Curated by {{ $show.curator }}</li>{{ end }}
    <li>{{ dateFormat "2 January 2006" $show.vernissage }}–{{ dateFormat "2 January 2006" $show.finissage }}</li>
  </ul>
{{ end }}

#10

indeed, this was the issue
thank you @zwbetz for your time

how did you think of this answer?
merci


#11

My original reply was just me spitballing, so the code wasn’t too clean. Once I cloned your repo and tested things out, I could see the issue.


#12

that is really thorough of you. thank you for putting in the time to clone the repo etc. i’m impressed and grateful.