In a Hugo partial template, how do I access secondary/additional parameters?
Asked Answered
F

1

16

Say I have {{ partial "li.html" $test $root.Data.Term }}.

With this I can can access the first parameter, or $test, by simply refering to . within the li.html template, but how do I access the second or additional parameter ($root.Data.Term) from within the same template?

Feasible answered 30/8, 2017 at 14:34 Comment(1)
Try {{ partial "li.html" (dict "test" $test "Term" $root.Data.Term ) }}, then access them with .test and .Term. Based-on: https://gohugo.io/functions/dict/Eyler
T
20

I would suggest using the hugo dict function. It allows you to use key/value pairs to pass information. The documentation states that for your use case.

{{ partial "yourPartial" (dict "test" "yourTestData" "term" "yourTerm") }}

You can then access the values by just using {{ .test }} and {{ .term }}.

Alternatively you can use the scratch function, which is a more "global" approach.

Telecommunication answered 17/6, 2018 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.