英文:
In a Hugo partial template, how do I access secondary/additional parameters?
问题
假设我有 {{ partial "li.html" $test $root.Data.Term }}。
通过这个语法,我可以在 li.html 模板中通过使用 . 来访问第一个参数 $test,但是如何在同一个模板中访问第二个或更多的参数($root.Data.Term)呢?
英文:
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?
答案1
得分: 16
我建议使用hugo的dict函数。它允许您使用键值对传递信息。根据文档,适用于您的用例的示例代码如下:
{{ partial "yourPartial" (dict "test" "yourTestData" "term" "yourTerm") }}
然后,您可以通过使用**{{ .test }}和{{ .term }}**来访问这些值。
另外,您还可以使用scratch函数,这是一种更"全局"的方法。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论