【问题标题】:Jekyll Collection Not RenderingJekyll 集合不渲染
【发布时间】:2023-03-21 20:27:01
【问题描述】:

我想为我的 jekyll 网站上的旧博客文章创建一个存档。以前,我的结构在我的网站主页 index.html 上提供_posts 的内容。在在线阅读了集合文档和一些教程之后,我在我的结构中添加了一个集合文件夹_archive,并在里面添加了一个名为test-file.markdown.的测试文件

但是,url mysite.com/archive/test-file 完全重新生成了我的主 index.html,而不是集合内容


结构:

_archive
    index.html
    test-file.markdown
_includes
    about.html
    head.html
    ... other stuff ...
_layouts
    default.html
_posts
    post1.markdown
    post2.markdown
    ... other stuff ...
css
img
js
_config.yaml
... other stuff ...

test-file.markdown

---
layout: default
title: test
---

_config.yml

# Site settings
title: test
email: test@test.com
url: http://www.test.com


# Color settings (hex-codes without the leading hash-tag)
color:
  primary: ffffff #80B3FF
  primary-rgb: "24,288,156" #"128,179,255"
  secondary: 2c3e50 #FD6E8A
  secondary-dark: 233140 #A2122F
  third: 979797

collections:
  archive:
    output: true
    permalink: /archive/:path/

# Build settings
markdown: kramdown
permalink: pretty

mysite.com/archive/index.html

---
---
{% for p in site.archive %}
  {{ p}}
  {{ p.title }}
{% endfor %}

这会重新渲染主 index.html,而不是 test-file.markdown 的内容。


如何在 mysite.com/archive/正确呈现_archive 的内容?


编辑:将--- 添加到 index.html

【问题讨论】:

    标签: jekyll liquid templating


    【解决方案1】:

    如果不查看整个网站,很难说出您的问题到底是什么。如果可能,请提供一个 repo URL。


    如果/index.html 的内容出现在/_archive/test-file.markdown 输出中,则post 循环可能在默认布局文件中,因为两个文件共享该布局。这里的解决方案是将相关内容移动到/index.html


    我相信您的/_archive/index.html 没有被输出。将/_archive/index.html 移动到/archive.html。 Jekyll 不处理 _archive 文件夹内的页面,因为它以下划线开头。

    然后,您将使用当前配置输出这些文件:

    • /archive/index.html
    • /archive/test-file/index.html

    在我看来,您应该将帖子保留为帖子,无论它们是否已归档。然后,您可以在归档帖子时保留 URL,而不必设置 301 或将它们丢失到巨大的空白中。

    为此,请在您的存档帖子中添加前文:

    ---
    archived: true
    ---
    

    以及您当前的帖子(您可以使用defaults 来避免重复):

    ---
    archived: false
    ---
    

    在您的主帖子循环中排除已归档的帖子:

    {% assign posts = site.posts | where: "archived", false %}
    

    排除存档页面上的当前帖子:

    {% assign archived_posts = site.posts | where: "archived", true %}
    

    【讨论】:

      【解决方案2】:

      您是否添加了:

      ---
      ---
      
      {% for p in site.archive %}
      {{ p}}
      {{ p.title }}
      {% endfor %}
      

      在 index.html 文件的顶部?如果它丢失,它将不会通过 jekyll 的模板引擎运行该文件中的任何内容。

      【讨论】:

      • 好的,谢谢。该站点仍在将我的整个 _posts 文件夹提供到 url:site.com/archive/test/
      • 这里还有什么我可能遗漏的吗?感谢@arniekoz 的任何想法。
      • 不,不要包装内容。但是在@NickB 上面做编辑看看它是否有效。
      • 谢谢,但仍在重新渲染 _posts 的内容,而不是 _archive :( 我认为这一定是 config.yml 或一般结构中的错误?感谢您的任何想法
      • 我总是为这些东西创建布局。将 index.html 放在布局目录中(作为 archives.html)并从 index.md 文件中引用它。希望对您有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多