我正在尝试创建 HTML 布局部分,以使用 front Matter 作为数据源和用于渲染的 Hugo 模板来添加不同的 html 元素。 例如前面的内容有这个属性
---
animal:
type:"bear"
---
现在部分看起来像这样:
{{ if eq .Params "animal.type" "dog" | or eq .Params "animal.type" "bear" | or eq .Params "animal.type" "fox" | or eq .Params "animal.type" "wolf" }}
<p> Belongs to the Canine family, they have distinctive fangs. <p>
{{ end }}
当我运行hugo server
时抛出以下错误:
executing "partials/canine.html" at : wrong number of args for eq: want at least 1 got 0
我对或运算符的管道操作是否错误?或者说有什么问题?
文件夹结构如下所示: single.html 正在调用 animalsDetails.html
这个正在调用 canine.html
我不认为它是问题,但以防万一。
|layouts
|_defaults
single.html
|partials
animalDetails.html
canine.html
最佳答案
您可能需要正确使用 .Params
并确保使用圆括号。
试试这个(未测试):
{{ if eq .Params.animal.type "dog" | or (eq .Params.animal.type "bear") | or (eq .Params.animal.type "fox") | or (eq .Params.animal.type "wolf") }}
<p> Belongs to the Canine family, they have distinctive fangs. <p>
{{ end }}
我建议不要在 Hugo 表达式中使用条形图,我在使用它们时遇到了问题(尽管这是 ReplaceRE 的一个例子)。
这是不使用栏的解决方案:
{{ if or (eq .Params.animal.type "dog") (eq .Params.animal.type "bear") (eq .Params.animal.type "fox") (eq .Params.animal.type "wolf") }}
<p> Belongs to the Canine family, they have distinctive fangs. <p>
{{ end }}
如果您需要更多帮助,请随时发表评论! 🙃
编辑 1:修复了拼写错误并添加了替代推荐解决方案。
关于if-statement - golang 中的 if 语句 Hugo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63499549/