golang hugo 中的 if 语句

if statments in golang hugo

我正在尝试创建 HTML 部分布局以添加不同的 html 元素,使用前端作为数据源并使用 hugo 模板进行渲染。 例如前面的内容有这个 属性

---
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 的情况)。

下面是解决方法w/o使用吧:

{{ 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 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 }}

做到了