【问题标题】:hugo won't render single page layout, instead uses _default single layouthugo 不会渲染单页布局,而是使用 _default 单页布局
【发布时间】:2020-02-02 20:27:35
【问题描述】:

)

我正在尝试为 hugo.io 创建自己的主题。到目前为止,一切正常。我唯一的问题是,我无法让 hugo 为单节内容呈现自定义布局。

对于帖子,这很好用:

  • index.html 被正确调用
  • “布局/帖子”中的 single.html 被正确调用
  • 对于教程,列表布局可以正常工作
  • 所有文件和列表的永久链接都能正常工作

问题: * 对于教程,单一布局不调用“layouts/tutorials/single.html”布局。相反,它使用“layouts/_default/single.html”

我尝试了以下方法:

  • 将 type="tutorial" 前言设置添加到所有教程 .md 文件中
  • 为所有教程 .md 文件添加 layout="tutorial" 前言设置
  • 在“layouts/_default”文件夹中添加一个“tutorial.html”文件(也尝试将其命名为“tutorials.html”)
  • 在“layouts”文件夹中添加一个“tutorial.html”文件(同上)

遗憾的是,这些都不起作用

我的内容设置:

content
|__ posts
   |__ new_post.md
|__ tutorials
   |__ new-tutorial.md

我的布局设置(在我的主题文件夹中):

layouts
|__ _default
   |__ list.html
   |__ single.html
|__ posts
   |__ list.html
   |__ single.html
|__ tutorials
   |__ list.html
   |__ single.html
|__index.html

我的单篇教程内​​容(new-tutorial.md):

+++
title = "My new Tutorial"
date: 2019-10-04T14:10:46+02:00
draft: false
type: tutorial
layout: tutorial
+++

# Custom Content Headline

hugo 没有错误信息。 我希望雨果打开页面“http://localhost:1313/tutorials/new-tutorial/”,其布局位于文件“layouts/tutorials/single.html”中

【问题讨论】:

  • 最后我尝试重新设置整个项目。在我创建了一个新站点/项目并创建了一个新主题并完成了所有内容和主题文件的整个复制和粘贴之后......一切都再次正常。所以我认为问题不在于讨论的文件中?
  • 你有没有想过这个问题?我面临着类似的问题,正在寻找线索。
  • @JensZalzala - 你解决了吗?我有同样的问题..

标签: hugo


【解决方案1】:

您不应在 new-tutorial.md 中指定 type: tutorialtype:tutorial

Hugo 默认使用 layouts/tutorials/single.html 作为教程部分,见https://gohugo.io/templates/lookup-order/#examples-layout-lookup-for-regular-pages

new-tutorial.md 的最终结果如下所示

+++
title = "My new Tutorial"
date: 2019-10-04T14:10:46+02:00
draft: false
+++

# Custom Content Headline

【讨论】:

  • 感谢您的帮助。可悲的是,在您的模式之后创建文件的标题对我不起作用;(我认为还有其他事情发生。
  • 我注意到 '+++' 部分中的元数据存在问题,在您的情况下它应该是 TOML 格式(请参阅文档gohugo.io/content-management/front-matter/#front-matter-formats)尝试使用 `` +++ title = "My new教程”日期 = 2019-10-04T14:10:46+02:00 草稿 = false +++ ```
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多