在Hugo的部分模板中,如何访问辅助/附加参数?

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2017年8月30日 22:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/45963231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定