Fetch a data file based on the page name

Hi there

I’m beginning with Hugo and trying to understand the concepts :slightly_smiling:

I’m trying to automate some rendering in my posts where i want to display and format a list of infos about the content.
I’ve stored this info in a data file (data/post-1.yaml, data/post-2.yaml).

In post-1.md and post-2.md i want to insert a shortcode to parse automatically the data for the current page.

{{$datasource := .Page.Title | urlize}}

    {{ range .Site.Data.$datasource }}
  • {{ . }}
  • {{ end }}

This is of course not working, is there a way to concatenate a variable in the range parameter, or to get the current page directly?

Thanks a lot

@zapatoche Just so I’m clear: is there any reason you’re not just putting the YAML at the top of each post as front matter?

An example from my site.

                {{ if isset .Site.Data.names .Title }}
                {{ $yaml := (index .Site.Data.names .Title) }}
                    <h1>{{ with $yaml.penname }} {{ index . }}
                    {{else}} {{ index $yaml.fullname }} {{ end }}</h1>
                    <div class="term-listing-heading">
                        {{ with $yaml.penname }} <strong>{{ index . }}</strong> {{ end }}
                        {{ with $yaml.nickname }} {{ index . }} {{ end }}
                        {{ with $yaml.years }} {{ index . }} {{ end }}
                        {{ with $yaml.info }} {{ index . }} {{ end }}
                        {{ with $yaml.pages }} <br /><details><summary>Страницы в 1 изд.:</summary> {{ index . }}</details>{{ end }}
                    </div>
                {{else}}
                    <h1>{{ replace .Title  "-" "–" }}</h1>
                {{ end }}

The data file ‘names.yaml’ is as follows.

John:
  fullname: John Smith
  shortname: J. Smith
  transname: john-smith
Нина Волохова:
  fullname: Волохова Нина Александровна
  shortname: Волохова Н. А.
  transname: volohova
  info: — Информация об авторе разыскивается!
  years:

@rdwatters

I want to abstract some of the tedious data away from whoever will edit the pages.
It contains loads of nested keys and repetition i need to iterate through so i want to keep the front matter “clean”.

Cheers

Thanks @Mikhail

Does .Site.Data.names contains all of the key and values for the different pages and can it be accessed based on the current page or is it something that still need to be passed as a hardcoded parameter?

As an alternative, is it possible to call the shortcode form the post.md with a path parameter?
{{< shortcodeName.Site.Data.post-1 >}}

This code generates a page title and a gray box on the righ for this page. As you can see the title is different from the url. URL=.Title, H1 generates from /data/names.yaml.

Thanks, i’ll give it a try :slight_smile: