Display author name (from data file)

Hello,

I have one data file per author in the _authors folder:

.ron.toml
.mark.toml

etc.

I also have a Params.author for each post, in the post frontmatter:

---
Title: "my post"
author: "ron"
---

How do I go about displaying the author full name, the email, and all the other data within the TOML data file?

I can do:

`{{ $author := .Site.Data._authors }}

{{ range $author }}
{{ if eq .name .Params.author }}
{{ .name }}
{{ end }}
{{ end }}`

To search within the “/_authors/” folder, but it is not showing me the name, for instance. From there how do I go about comparing the .Params.author from the frontmatter of each post, against the name of the datafile?

It eludes me!

What does the data in your toml files look like? do they also have an author field? That is how you would match it not with the filename IIRC.

Just looked at my old site where I was using data here is a sample of my range.

{{ if isset .Params "author" }}
            {{ range .Site.Data.authors }}
                {{ $author := lower $.Params.author }}
                {{ $pageAuthor := lower .author }}
                {{ if eq $author $pageAuthor }}
                    {{ partial "author" . }}
                {{ end }}
            {{ end }}
        {{ end }}

And this is what a toml file looks like.

author = "author"
name = "Full Name"
title = "title goes here"
bio = "Bio goes here"

Hi Ben,

My data looks like:

_file name: /data/_authors/thomas.toml

name = "Thomas Edison"
title = "editor"
short_bio = "short bio goes here"

_file name: /data/_authors/tesla.toml

name = "Nikolai Tesla"
title = "junior journalist"
short_bio = "short bio goes here"

What I was hoping was to match the file name (or at least the name) with the name of the author in the frontmatter.

article front matter:


Title: “my article”
name: “Thomas”

Hi,

I think this example is similar to what you are trying to do: https://gohugo.io/functions/index-function/#example-load-data-from-a-path-based-on-front-matter-params

Using the info provided by @pointyfar should solve your problem so get rid of your range and just do this to render your fields:

{{if isset .Params "author"}}  
    {{(index .Site.Data._authors .Params.author).name}}
    {{(index .Site.Data._authors .Params.author).title}}
    {{(index .Site.Data._authors .Params.author).short_bio}}
{{end}}

I wish I knew about this when I was using data it is much easier than what I did and probably more performant.

2 Likes

Thank you, very, very much. That really made my day a LOT more easier, especially after a family emergency that ate a large chunk of my week.

Here, have a cookie:

THANKS AGAIN!

Thank you A LOT! I’ve been struggling with this for day. You and Ben just made my weekend a lot more bearable. Much appreciated!

Here, have a cookie:

1 Like

Glad we could help :slight_smile:

You can dry this out a bit with:

{{ $author := index .Site.Data.authors .Params.author }}

Then just call it as:

$author.name

etc…

Sorry to revive a dead thread, just posting for anyone who stumbles on this later.