我正在使用Hugo Universal Theme。我是静态站点生成器的新手。这个问题是给熟悉hugo模板的人的。
在layouts/partials/features.html
中,我们可以看到$element.name
和$element.name.description
的渲染位置:
{{ if isset .Site.Params "features" }}
{{ if .Site.Params.features.enable }}
{{ if gt (len .Site.Data.features) 0 }}
<section class="bar background-white">
<div class="container">
{{ range $index, $element := sort .Site.Data.features "weight" }}
{{ if eq (mod $index 3) 0 }}
<div class="col-md-12">
<div class="row">
{{ end }}
<div class="col-md-4">
<div class="box-simple">
<div class="icon">
<i class="{{ .icon }}"></i>
</div>
<h3>{{ $element.name }}</h3>
<p>{{ $element.description | markdownify }}</p>
</div>
</div>
{{ if or (eq (mod $index 3) 2) (eq $index (sub (len $.Site.Data.features) 1 )) }}
</div>
</div>
{{ end }}
{{ end }}
</div>
</section>
{{ end }}
{{ end }}
{{ end }}
在这种情况下要呈现的数据在data/features/consulting.yaml
中定义如下:
weight: 4
name: "Consulting"
icon: "fa fa-lightbulb-o"
description: "Fifth abundantly made Give sixth hath..."
我该怎么做才能将新变量添加到yaml
文件中,然后在hugo编译站点时可以通过html
文件进行渲染。我试图简单地添加另一个参数param1
,然后在说明段下面的<p>{{ $element.param1 | markdownify }}</p>
处的html文件中插入相应的行,但出现错误ERROR 2018/08/23 10:42:42 Error while rendering "home" in "": template: index.html:22:11: executing "index.html" at <partial "features.ht...>: error calling partial: template: partials/features.html:18:56: executing "partials/features.html" at <markdownify>: wrong number of args for markdownify: want 1 got 0
很明显,我似乎无法正确定义变量,但是应该在哪里做呢?我可以向config.toml
添加另一个站点变量,但是我想学习如何制作可以在yaml/frontmatter
类型条目中定义的页面特定变量。我尝试阅读有关hugo变量的信息,但对什么是变量和什么是简码感到困惑。非常感谢您对本示例的帮助。
答案 0 :(得分:0)
好吧,我找到了一个可行的答案,但我仍然不完全了解它如何与Hugo变量系统配合使用,因此,欢迎提供更好的答案和/或评论。
它看起来很简单。我必须在url
文件中定义yaml
变量:
name: "History"
position: "Hx"
url: "/blog/2018/08/23/01-history/"
,然后像这样在html
文件中使用:
{{ if .url }}
<a href="{{ .url }}">
<h5>{{ .name }}</h5>
</a>
{{ else }}
<h5>{{ .name }}</h5>
{{ end }}
如果.yaml中定义了.url,它的工作是将.name放在链接标记中。如果给出了绝对URL,这也可以工作。因此,似乎页面变量被称为.myVariable
。模板作者在上面的另一个地方使用了$element.name
,这让我感到困惑。
我也可以将在前题中定义的参数称为.Params.name
我在https://github.com/devcows/hugo-universal-theme/pull/166找到了指针,并在调整模板时进行了测试;效果很好。