网站构建失败,原因是在类型 toml.LocalDate 上不可用 Format() 方法。

huangapple go评论80阅读模式
英文:

Site build fails due to Format() not being available on Type toml.LocalDate

问题

当我使用Hugo构建一个使用流水线的网站时,构建过程经常失败,并显示以下错误:

Error: Error building site: 
failed to render pages: render of "home" failed: execute of template failed: template: index.html:22:11: executing "main" at <partial "session-dates.html" .>:
error calling partial: "/github/workspace/layouts/partials/session-dates.html:4:20":
execute of template failed: template: partials/session-dates.html:4:20:
executing "partials/session-dates.html" at <.Site.Params.current_startDate.Format>: can't evaluate field Format in type toml.LocalDate

错误发生在current_startDatecurrent_endDate的值上,错误信息显示该类型没有可用的Format函数。配置如下:

baseURL = "..."
languageCode = "en-us"
title = "..."
summaryLength = 20
relativeURLs = true

[params]
    current_startDate = 2021-10-10
    current_endDate   = 2021-12-04

然后模板代码如下:

<div class="row">
    <div class="col-lg-12 dates">
        <span class="long-dates">
            {{ .Site.Params.current_startDate.Format "January 2, 2006" }} —
            {{ .Site.Params.current_endDate.Format "January 2, 2006" }}
        </span>
        <span class="short-dates">
            {{ .Site.Params.current_startDate.Format "01/02/06" }} —
            {{ .Site.Params.current_endDate.Format "01/02/06" }}
        </span>
    </div>
</div>

我已经尝试查看文档和其他示例,配置似乎是正确的,所以我认为可能有一些明显的遗漏。

英文:

When I am building a site on Hugo using a pipeline, the build constantly fails with the following error:

Error: Error building site: 
failed to render pages: render of &quot;home&quot; failed: execute of template failed: template: index.html:22:11: executing &quot;main&quot; at &lt;partial &quot;session-dates.html&quot; .&gt;: 
error calling partial: &quot;/github/workspace/layouts/partials/session-dates.html:4:20&quot;: 
execute of template failed: template: partials/session-dates.html:4:20: 
executing &quot;partials/session-dates.html&quot; at &lt;.Site.Params.current_startDate.Format&gt;: can&#39;t evaluate field Format in type toml.LocalDate

The error is happening on the current_startDate, and current_endDate values, where it's saying there is no Format function available for the type. The config is:

baseURL = &quot;...&quot;
languageCode = &quot;en-us&quot;
title = &quot;...&quot;
summaryLength = 20
relativeURLs = true

[params]
    current_startDate = 2021-10-10
    current_endDate   = 2021-12-04

Then the template code is:

&lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-lg-12 dates&quot;&gt;
        &lt;span class=&quot;long-dates&quot;&gt;
            {{ .Site.Params.current_startDate.Format &quot;January 2, 2006&quot; }} —
            {{ .Site.Params.current_endDate.Format &quot;January 2, 2006&quot; }}
        &lt;/span&gt;
        &lt;span class=&quot;short-dates&quot;&gt;
            {{ .Site.Params.current_startDate.Format &quot;01/02/06&quot; }} —
            {{ .Site.Params.current_endDate.Format &quot;01/02/06&quot; }}
        &lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;

I have tried looking at the docs and other examples and the configuration seems to be correct, so I assume there is something obvious I am missing.

答案1

得分: 0

如果其他人遇到了这个问题,特别是在这个管道上https://github.com/chabad360/hugo-gh-pages,这个方法不起作用。在本地测试时,我原来的格式在命令行中的构建和预览都有效。

修复方法是给出完整的日期时间格式,所以对于我上面的例子,应该是:

baseURL = "..."
languageCode = "en-us"
title = "..."
summaryLength = 20
relativeURLs = true

[params]
    current_startDate = 2021-10-10T14:15:59-06:00
    current_endDate   = 2021-12-04T14:15:59-06:00

这个配置在本地和管道中都能正常构建。

英文:

If anyone else is running into this issue, for some reason specifically on the pipeline https://github.com/chabad360/hugo-gh-pages this doesn't work. When testing locally the original format I had works for both building and previewing at the command line.

The fix is to give the full datetime format, so for my example above that is:

baseURL = &quot;...&quot;
languageCode = &quot;en-us&quot;
title = &quot;...&quot;
summaryLength = 20
relativeURLs = true

[params]
    current_startDate = 2021-10-10T14:15:59-06:00
    current_endDate   = 2021-12-04T14:15:59-06:00

This configuration builds as expected locally, and in the pipeline.

huangapple
  • 本文由 发表于 2021年9月16日 01:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/69197828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定