Can't evaluate field <field> in type interface {} from a shortcode

I currently have a basic post with the following expected behavior : use a parameter called reference to make the shortcode load dynamically the information from a yaml file in the data/post folder

---
title: 'title'
draft: true
featured: false
author: 'Chris'
reference: "002"
---
{{< conclusion >}}

The shortcode called conclusion :

{{ $data := index .Site.Data.post (.Page.Params.Get "reference") }}
{{ with $data.conclusion }}
<div style="">
  <p>{{ .text }}</p>
</div>
{{ end }}

And a “data” file called *002.yml’ in the data/post folder :

conclusion:
  - text: "bla bla"
  - rate: 18

Unfortunatly i can’t access the text part of the conclusion and can’t figure out why :

execute of template failed: template: shortcodes/conclusion.html:4:8: executing "shortcodes/conclusion.html" at <.text>: can't evaluate field text in type []interface {}

The content of $data is correct if i print it

No:

{{ $data := index .Site.Data.post (.Page.Params.Get "reference") }}

Yes:

{{ $data := index .Site.Data.post .Page.Params.reference }}

Use the .Get method to “get” parameters passed to the shortcode, not to to “get” other things.

Thanks for the improvement
The first line of the shortcode is now :

{{ $data := index .Site.Data.post .Page.Params.reference }}

However the error is still here

I didn’t notice that your data file was malformed.

With this markdown:

+++
title = 'Post 1'
date = 2023-04-16T11:51:23-07:00
draft = false
reference = '002'
+++

{{< conclusion >}}

And this shortcode:

{{ $data := index .Site.Data.post .Page.Params.reference }}
{{ with $data.conclusion }}
  <div style="">
    <p>{{ .text }}</p>
  </div>
{{ end }}

And this data file:

conclusion:
  text: "bla bla"
  rate: 18

I get “bla bla”

1 Like

Bingo ! The array was not the structure i wanted ! Thank you for your time and help !

1 Like

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