if-statement - golang 中的 if 语句 Hugo

标签 if-statement hugo

我正在尝试创建 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/

相关文章:

swift - 转到下一个 ViewController 并在条件有效时传递数据

java - 尝试使用java中的扫描仪读取回车键的敲击作为输入?

r - 在 blogdown 中找不到 Hugo 主题目录

amazon-web-services - 如何将环境变量添加到 CodeBuild buildspec.yml?

javascript - 当只能使用html、css和jas时为静态网站创建弹出窗口

mysql - If 条件 UPDATE else INSERT

PHP If/Else 语句在 While 循环中不起作用

javascript - 为什么我的嵌套 if 和 else 语句不起作用?

static-site - Netlify:如何串行运行构建任务?

github - (Hugo) 从原始 GitHub 加载动态内容