dreamMyDream2014 2019-04-16 16:29
浏览 84
已采纳

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

I am new to Hugo, know nothing about GoLang and I am trying to do the following.

Problem

I have a Hugo site, and in my posts, I specify keywords in the front matter like:

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

In my template, I want to add a <meta> for keywords, so I have:

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

The problem, of course, is that I get this in the output:

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

While my objective is to get:

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

How to achieve this?


What I have tried

Looking at this documentation, I have tried to play a little:

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

Also tried:

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

They do not work. Also tried:

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

This last one causes an error:

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

  • 写回答

2条回答 默认 最新

  • douqian2957 2019-04-16 16:34
    关注

    Can you try

    <p>Keywords: {{ delimit .Keywords ", " }}</p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试