How to access nested object value

Hello,

I’m trying to iterate over the target variable, to display the values in a partial template. The template only needs information from the given context.

Here is the simplified structure of my frontmatter :

title: "Title of the page"
formationSection:
  - name: "Formation 1"
    image: "/images/formation.png"
    target:
      title: "Who can participate"
      items:
        - "Developers"
        - "Product Owners"
        - "UX Designers"
  - name: "Formation 2"
    image: "/images/formation-2.png"
    target:
      title: "Who can participate"
      items:
        - "Developers"

I pass the corresponding context in my single.html, like so :

<main class="sl-offer">
    {{ with .Params.formationSection }}
        {{- partial "formation-section.html" . -}}
    {{ end }}
</main>

Then, in my formation-section.html I’m able to display first level variables, like name and image :

<section class="formation">
    <div class="tabs">
            <ul class="tabs__selectors">
                {{ range . }}
                    <li>
                        <button type="button">{{- .name -}}</button> <!-- prints correctly the name -->
                    </li>
                {{ end }}
            </ul>
        <div class="tabs__content">
                {{ range . }}
                    <div class="tab">
                        {{ .target.title }}
                        <ul>
                            {{ range .target.items }}
                                <li> {{ . }}</li>
                            {{ end }}
                        </ul>
                    </div>
                {{ end }}
        </div>
    </div>
</section>

I’m then confronted to this error : <.target.title>: can't evaluate field title in type interface {} , when I try to access the variables in the target object

What would be the correct syntax ? I tried playing with the context but it didn’t change anything.

Thank you for your help

At first glance it appears that the context is lost when trying to access the parameter.

From within the partial try assigning a variable to the parameter like so:
{{- $param := $.Params.formationSection -}}

Then iterate over it with: {{ range $param }}.

I put your code at the top of my partial :

{{ $param := $.Params.formationSection }}

{{ range $param }}
    {{ . }}
{{ end }}

But still got the error :

<$.Params.formationSection>: can't evaluate field Params in type []interface {}

Can you post a non-simplified version of your front matter?

I am unable to reproduce the problem with the front matter and code that you have posted.

Sure, here it is :

Note : Edited to avoid indexation by search engines

I still cannot reproduce the problem locally using your data and code. Please post a link to the public repository for your project, or to a repository that demonstrates the problem.

Unfortunately I can’t do that, i thank you for your help though. The fact that you can’t reproduce the issue is already a clue for me. I will try some things and update the topic when I find the solution.

Just to be clear, should it work with this syntax from my original post ? : {{ .target.title }}

Your original syntax produces the expected results. No errors.

1 Like

Mystery solved : another markdown (translated version) didn’t have the same structure and some keys were missing.

Thank you all for your time.

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