【问题标题】:Hugo won't let me list pages from a section, rejecting arguments?雨果不会让我列出某个部分的页面,拒绝争论?
【发布时间】:2021-11-13 16:43:23
【问题描述】:

layouts/index.html:

{{define "main"}}
{{ $products := .Site.RegularPages.ByTitle "Section" "products" }}
{{with .Params.banner}}
[...]
              <div class="dropdown-menu">
                  {{ range (where $products ) }}
                    <a class="dropdown-item " href="{{ .Permalink }}">{{ .Title }}</a>
                  {{ end }}
[...]

产生这个错误:

ERROR: executing "main" at <.Site.RegularPages.ByTitle>: wrong number of args for ByTitle: want 0 got 2

好的,让我们尝试删除我猜的那两个(必要的)参数?

{{ $products := .Site.RegularPages.ByTitle }}

现在,错误提示:

ERROR: executing "main" at <where>: wrong number of args for where: want at least 2 got 1

这里发生了什么?第一段代码在我的layouts/header.html 中运行良好,但现在 Hugo 似乎很困惑。

【问题讨论】:

    标签: javascript hugo


    【解决方案1】:

    您缺少 where 语句。 $products 是一个变量

    .Site.RegularPages.ByTitle <- isn't a value. 
    

    因此错误很明显,不是吗?

    但是要处理你的原点:

    在分配变量和运行函数之间的中途。

    所以:

    $products := .Site.RegularPages <- existing "Array/Map/Slice" assigned to a variable
    

    .ByTitle 是传递给函数的参数(我相信)。

    如果您希望按标题组织分组 - 这将是一个在分配给变量之前运行的函数。

    按您想要的标题浏览常规页面:

    {{range .Site.RegularPages.ByTitle}}
    {{ . }}
    {{ end }}
    

    如果你仍然想要一个按标题排序的“数组”: 请参阅文档:

    https://gohugo.io/functions/where/ (which I find to be excellent) and 
    https://gohugo.io/functions/sort/#readout
    

    你会使用排序。你也可以这样做:

    {{range where .Site.RegularPages.ByTitle "Section" "products" }}
     {{ . }}
    {{ end }}
    

    或者更具体地说:

    {{ $product := where .Site.RegularPages.ByTitle "Section" .Section }}
    

    总结: 要回答您的问题: 您的代码不起作用,因为您将分配变量与运行函数混合在一起。

    在变量的赋值中你几乎有一个 where 语句。这就是您收到错误的原因。

    补充说明:

    由于我无权访问您的存储库,我真的不知道您要做什么,我建议您使用上述文档来帮助您。

    我认为您要解决的“早期错误”是 .Params.Banners 的“with”语句,因为您正在“删除上下文”,我相信它们的逻辑存在错误。

    但是,如果您仍然想解决这个问题,请使用“排序”对值或 $products 进行排序(使用 where 语句为其分配常规页面)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 2021-08-03
      • 2013-07-16
      相关资源
      最近更新 更多