You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hugo框架向Partial传递字符串数组报错求助

问题排查与解决

错误原因

自定义的Front Matter字段无法直接通过page.Page对象访问,必须通过.Params属性获取。你在layouts中直接使用.Typedjstitle,而Hugo的page.Page类型并没有这个内置字段,因此触发报错。

具体修复步骤

1. 修改layouts文件中的Partial调用

将自定义字段的访问方式改为.Params.xxx格式,因为所有自定义Front Matter字段都归类在页面参数Params下:

<div class="intro">
  <h1>
    {{ .Title }} {{ partial "typedjs.html" .Params.typedjstitle }}{{ .Params.endtitle }}
    {{ if .Site.Params.addDot }}<span class="dot">.</span>{{ end }}
  </h1>
  <div class="intro-content">{{ .Content }}</div>
</div>

(注:同时把.Endtitle改为.Params.endtitle,endtitle同样是自定义字段,需通过Params访问)

2. 优化Partial的HTML结构(可选但推荐)

原Partial中使用<h1 id="typed-title"></h1>会导致<h1>嵌套<h1>的HTML语义错误,建议替换为行内元素:

<span id="typed-title"></span>
{{ $strings := . }}
<script>
  var typed = new Typed("#typed-title", {
    strings: {{ $strings }},
    typeSpeed: 100,
    loop: true,
    cursorChar: "_",
  });
</script>

验证修复

修改完成后重新运行Hugo,模板将正确读取Front Matter中的typedjstitle字符串数组,Typed.js可正常渲染轮换文本。

内容的提问来源于stack exchange,提问作者infodev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 19:45:29