Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 851
01AutoMonkey Avatar asked Aug 30 '17 14:08

01AutoMonkey


People also ask

What is partials in Hugo?

Partials are smaller, context-aware components in your list and page templates that can be used economically to keep your templating DRY.

What are template partials?

Template partials are small bits of reusable template or tag parts. You could create a Template partial for any number of purposes, anywhere that you need to reuse a small portion of a template, including partial or complete tags, other variables, etc.

What are partials HTML?

Partials often refers to HTML that is not a complete web page, but is intended for re-use within multiple web pages. Common examples are the header and the footer of site pages.


1 Answers

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.

like image 67
Boehrsi Avatar answered Oct 11 '22 23:10

Boehrsi