html - 如何在 Hugo 模板中将值列表转换为逗号分隔的字符串

标签 html go hugo

我是 Hugo 的新手,对 GoLang 一无所知,我正在尝试执行以下操作。

问题

我有一个 Hugo 网站,在我的帖子中,我指定了 keywords在前面的事情是这样的:

---
author: Andrea Tino
keywords:
- language
- image
- fun
---

在我的模板中,我想添加一个 <meta>对于关键字,所以我有:

<head>
    <meta charset="utf-8">
    {{ if .Keywords }}
    <meta name="keywords" content="{{ .Keywords }}">
    {{ end }}
    <title>{{ .Title }} | {{ .Site.Title }}</title>
</head>

当然,问题是我在输出中得到了这个:

<head>
    <meta charset="utf-8">
    <meta name="keywords" content="[language image fun]">
    <title>{{ .Title }} | {{ .Site.Title }}</title>
</head>

虽然我的目标是:

<meta name="keywords" content="language, image, fun">

如何实现?


我尝试过的

看着这个documentation ,我试玩了一下:

{{ if .Keywords }}
<meta name="keywords" content="{{ .Keywords | println }}">
{{ end }}

还试过:

{{ if .Keywords }}
<meta name="keywords" content="{{ .Keywords | printf "%s" }}">
{{ end }}

它们不起作用。还试过:

{{ if .Keywords }}
<meta name="keywords" content="{{ println(strings.Join(.Keywords, ", ")) }}">
{{ end }}

最后一个会导致错误:

Error: "/Users/me/Git/myproj/themes/mytheme/layouts/partials/header.html:7:1": parse failed: template: partials/header.html:7: unexpected "(" in operand

最佳答案

你能试试吗

<p>Keywords: {{ delimit .Keywords ", " }}</p>

关于html - 如何在 Hugo 模板中将值列表转换为逗号分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55712785/

相关文章:

go - 操作数中出现意外的 "="

javascript - 即使显示 :none?,div 中的 div 仍然占用空间

go - 如何以惯用方式构建现有包的扩展

github - 由于 YAML 错误,Hugo 无法使用 GitHub Pages 构建

docker - 云运行和发布容器

unit-testing - 如何在读取响应正文时强制出错

go - 如何结合 go range 中的 where 和 first

javascript - 如何使用 jQuery 重置 CSS?

css - 让图像与 div 重叠并位于文本后面

javascript - 设计谷歌日历月 View