How to get array element by index?

Hi.
I need to add a few links with pdf file for the post.
In the front-matter i add the field file as array. The first index is the file name, the second - path.

title: "Villacher"
date: 2020-03-03T15:24:44+02:00
subtitle: "Il birrifiocio storico della Carinzia"
linkText: www.villacher.com
files: [["VILLACHER Selection Red", "Scheda-Villacher-Selection-Red.pdf"], ["VILLACHER Pur", "Scheda-Villacher-Pur.pdf"]]
draft: true

I want to put in the link the file name and the path.

            <ul class="single-product__list">
                {{ range $elem_index, $elem_val := .Params.files }}
                    {{ range $sub_elem_index, $sub_elem_val := $elem_val}}
                        <li>
                            <a class="link link--reverse" href="#">
                                <span>{{ $sub_elem_val }}</span>
                                <i class="fas fa-chevron-circle-right"></i>
                            </a>
                        </li>
                    {{ end }}
                {{ end }}
            </ul>

How to do that?

This is my test project https://github.com/seriiserii825/h-test

1 Like

In Go (and Go templates) there are two core list structures: Slices and maps. In Go templates you access a specific element in both of these using the index func.

1 Like

Thanks, i find a solution.
{{ range $elem_index, $elem_val := .Params.files }}

                {{ $linkText := index ($elem_val) 0 }}
                {{ $link := index ($elem_val) 1 }}

                <li>
                    <a class="link link--reverse" href="{{ $link | absURL }}">
                        <span>{{ $linkText }}</span>
                        <i class="fas fa-chevron-circle-right"></i>
                    </a>
                </li>


                {{ end }}

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