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

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

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

问题

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

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

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

  1. baseURL = "..."
  2. languageCode = "en-us"
  3. title = "..."
  4. summaryLength = 20
  5. relativeURLs = true
  6. [params]
  7. current_startDate = 2021-10-10
  8. current_endDate = 2021-12-04

然后模板代码如下:

  1. <div class="row">
  2. <div class="col-lg-12 dates">
  3. <span class="long-dates">
  4. {{ .Site.Params.current_startDate.Format "January 2, 2006" }} —
  5. {{ .Site.Params.current_endDate.Format "January 2, 2006" }}
  6. </span>
  7. <span class="short-dates">
  8. {{ .Site.Params.current_startDate.Format "01/02/06" }} —
  9. {{ .Site.Params.current_endDate.Format "01/02/06" }}
  10. </span>
  11. </div>
  12. </div>

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

英文:

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

  1. Error: Error building site:
  2. 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;:
  3. error calling partial: &quot;/github/workspace/layouts/partials/session-dates.html:4:20&quot;:
  4. execute of template failed: template: partials/session-dates.html:4:20:
  5. 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:

  1. baseURL = &quot;...&quot;
  2. languageCode = &quot;en-us&quot;
  3. title = &quot;...&quot;
  4. summaryLength = 20
  5. relativeURLs = true
  6. [params]
  7. current_startDate = 2021-10-10
  8. current_endDate = 2021-12-04

Then the template code is:

  1. &lt;div class=&quot;row&quot;&gt;
  2. &lt;div class=&quot;col-lg-12 dates&quot;&gt;
  3. &lt;span class=&quot;long-dates&quot;&gt;
  4. {{ .Site.Params.current_startDate.Format &quot;January 2, 2006&quot; }}
  5. {{ .Site.Params.current_endDate.Format &quot;January 2, 2006&quot; }}
  6. &lt;/span&gt;
  7. &lt;span class=&quot;short-dates&quot;&gt;
  8. {{ .Site.Params.current_startDate.Format &quot;01/02/06&quot; }}
  9. {{ .Site.Params.current_endDate.Format &quot;01/02/06&quot; }}
  10. &lt;/span&gt;
  11. &lt;/div&gt;
  12. &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,这个方法不起作用。在本地测试时,我原来的格式在命令行中的构建和预览都有效。

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

  1. baseURL = "..."
  2. languageCode = "en-us"
  3. title = "..."
  4. summaryLength = 20
  5. relativeURLs = true
  6. [params]
  7. current_startDate = 2021-10-10T14:15:59-06:00
  8. 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:

  1. baseURL = &quot;...&quot;
  2. languageCode = &quot;en-us&quot;
  3. title = &quot;...&quot;
  4. summaryLength = 20
  5. relativeURLs = true
  6. [params]
  7. current_startDate = 2021-10-10T14:15:59-06:00
  8. 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:

确定