非站点级变量/参数查询的Hugo定义

我正在使用 Hugo 通用主题。我是静态站点生成器的新手。这个问题是为熟悉 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,以便稍后html在 hugo 编译站点时通过文件呈现。我试图简单地添加另一个参数param1,然后在 html 文件中插入相应的行,<p>{{ $element.param1 | markdownify }}</p>如下描述段落但出现错误


错误 2018/08/23 10:42:42 在“”中呈现“主页”时出错:模板:index.html:22:11:在<部分“features.ht ...> 处执行“index.html”:错误调用部分:模板:partials/features.html:18:56:在以下位置执行“partials/features.html”:markdownify 的参数数量错误:想要 1 得到 0


显然我似乎无法正确定义变量,但我应该在哪里做呢?我可以向 中添加另一个站点变量config.toml,但我想了解如何制作可在yaml/frontmatter类型条目中定义的页面特定变量。我尝试阅读有关 hugo 变量的内容,但在什么是变量和什么是短代码方面陷入困境。非常感谢您对此示例的帮助。


去雨果雨果简码


犯罪嫌疑人X
浏览 108回答 1
1回答

ITMISS

好吧,我找到了一个有效的答案,但我仍然不完全理解它如何适应 Hugo 变量系统,因此非常欢迎更好的答案和/或评论。看起来很简单。我必须url在文件中定义变量yaml:name: "History"position: "Hx"url: "/blog/2018/08/23/01-history/"然后html 像这样在文件中使用:{{ if .url }}&nbsp;&nbsp; &nbsp; <a href="{{ .url }}">&nbsp;&nbsp; &nbsp; <h5>{{ .name }}</h5>&nbsp; &nbsp; </a>{{ else }}&nbsp; &nbsp; <h5>{{ .name }}</h5>{{ end }}它所做的是将 .name 放在链接标记中,如果 .url 是在 .yaml 中定义的。如果给出绝对 URL,这也有效。因此,页面变量似乎被称为.myVariable. $element.name如上所述,模板作者在另一个地方使用,这让我感到困惑。我也可以将 frontmatter 中定义的参数称为.Params.name我在https://github.com/devcows/hugo-universal-theme/pull/166找到了指针并测试了调整模板;它运作良好。
打开App,查看更多内容
随时随地看视频慕课网APP