【问题标题】:Jekyll issue with overlapping scopes in _config.yml_config.yml 中重叠范围的 Jekyll 问题
【发布时间】:2016-12-28 15:10:27
【问题描述】:

我在_config.yml 文件中使用范围时遇到问题:

在一个多语言博客中,我想为所有帖子使用给定的布局,然后为每种语言自定义具有更专业范围的永久链接。这是我的配置:

defaults:
    -
        # defaults for all files in the project
        scope:
            path: ""
        values:
            layout: my-page-layout
    -
        # defaults for all posts in the project
        scope:
            path: ""
            type: posts
        values:
            layout: my-post-layout
    -
        # defaults for all english posts in the project
        scope:
            path: en
            type: posts
        values:
            permalink: /en/:year/:month/:title/
    -
        # defaults for all french posts in the project
        scope:
            path: fr
            type: posts
        values:
            permalink: /fr/:year/:month/:title/

en 路径下的英文帖子实际上具有正确的布局 (my-post-layout),但路径 fr 下的法语帖子具有默认布局 (my-page-layout)。

看起来与“fr/posts”对对应的范围会覆盖所有帖子的默认值,而与“en/posts”对对应的范围并非如此。

我错过了什么?

编辑:

我的jekyll项目的目录结构是这样的(我删除了不相关的文件):

./
├──_layouts/
│  ├──my-page-layout.html
│  └──my-post-layout.html
│
├──en/
│  ├──_posts/
│  │  └──2016-12-01-my-post-in-english.md
│  │
│  └──my-page-in-english.html
│
├──fr/
│  ├──_posts/
│  │  └──2016-12-01-mon-post-en-français.md
│  │
│  └──ma-page-en-français.html
│
└──_config.yml

【问题讨论】:

    标签: jekyll


    【解决方案1】:

    编辑:

    从您的代码中读取:

    defaults:
        -
            scope:
                path: ""
            values:
                layout: wasthishelpful-page
                lang: en
        -
            scope:
                path: fr
            values:
                lang: fr
        -
            scope:
                path: ""
                type: posts
            values:
                layout: wasthishelpful-post
        -
            scope:
                path: en/_posts
            values:
                permalink: /en/:year/:month/:title/
        -
            scope:
                path: fr/_posts
            values:
                permalink: /fr/:year/:month/:title/
    

    jekyll code可以看出我们有一个优先级问题。

    第二条规则适用于 /fr 路径,但不会被适用于 / 路径的第三条规则覆盖。

    解决方案是通过反转它们来在第二条规则之前声明第三条规则。

    defaults:
        -
            scope:
                path: ""
            values:
                layout: wasthishelpful-page
                lang: en
        -
            scope:
                path: ""
                type: posts
            values:
                layout: wasthishelpful-post
        -
            scope:
                path: fr
            values:
                lang: fr
        ...
    

    【讨论】:

    • 感谢您的回答。我将我的帖子存储在 en/_postsfr/posts 中(我编辑了我的问题以精确目录结构)。我试过你的提议,但它不起作用:英文帖子仍然有正确的布局,而法语帖子没有
    • 编辑了我的答案。您在 _posts 中错过了一个 s
    • 对不起,这是一个错字,就像我的评论一样。这些帖子在 en/_postsfr/_posts 中,这就是我在 _config.yml 中使用的路径,就像在您的回答中一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多