【问题标题】:Jekyll multiple pages using the same templateJekyll 多个页面使用相同的模板
【发布时间】:2017-12-05 10:58:54
【问题描述】:

我知道您可以使用 _layouts 并在您的页面中执行类似的操作

---
layout: some_layout
title: Home
---

假设我有 20 页。全部使用相同的模板,但里面的内容和内容略有不同。

而不是创建 20 个具有不同名称和不同永久链接的不同 pages.html 文件。

有没有办法创建 1 个 page.html 并根据永久链接更改 {{ content }} 中的内容?

【问题讨论】:

    标签: html jekyll


    【解决方案1】:

    只需创建 your-slug.md 文件。让他们都使用相同的布局,像这样:

    ---
    layout: some_layout
    title: Your title
    ---
    

    在布局文件 (some_layout.html) 中放置一些逻辑,如下所示:

    {% if page.url contains '/your-slug' %}Put this on the screen.{% endif %}
    

    【讨论】:

    • 这真的回答了这个问题吗?我认为原始海报要求一种方法来拥有一个 page.html(或者在你的情况下是一个 your-slug.md)而不是很多。
    • 这对我来说似乎是不可能的,除非您使用页面生成器。上面的布局技巧需要一个布局文件和 20 个内容相同的 .md 文件。
    【解决方案2】:

    您可以在一个集合下组织 20 个页面,并为该集合指定默认值。 例如,假设您的收藏被标记为 docs,那么所有这 20 个页面都需要放在源目录根目录下名为 _docs 的目录中。然后将您的集合配置为对其文档使用布局 some_layout

    # _config.yml
    
    # enable rendering on your collection(s)
    collections:
      docs:
        output: true
      another_collection:
        output: true
    
    # set defaults on your collection. (note the indentation..)
    defaults:
      -
        scope:
          type: docs
          path: _docs
        values:
          layout: some_layout # The layout for this collections' files
      -
        scope:
          type: another_collection
          [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      相关资源
      最近更新 更多