Jekyll - 更改帖子布局中的 _config.yml 变量

Joã*_*ssa 5 jekyll

在我的config.yml我有:

\n\n
...\n\nshowheader: "yes"\n
Run Code Online (Sandbox Code Playgroud)\n\n

而在我的default.html模板中,我有一个显示标题的条件,包括:

\n\n
<!DOCTYPE html>\n<html>\n  {% include head.html %}\n  <body>\n    {% if site.showheader == "yes" %}{% include header.html %}{% endif %}\n    <main class="page-main">\n      {{ content }}\n    </main>\n    {% include footer.html %}\n </body>\n</html>\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我的post.html模板中我不想显示header.html所以我尝试这样做:

\n\n
---\nlayout: default\n---\n{{ site.showheader = "no" }}\n<article class="post">\n  <header class="post-header">\n    <h1 class="post-h1" href="{{ post.url | prepend: site.baseurl }}">{{ page.title }}</h1>\n    <p class="post-meta">{{ page.date | date: "%b %-d, %Y" }}{% if page.author %} \xe2\x80\xa2 {{ page.author }}{% endif %}{% if page.meta %} \xe2\x80\xa2 {{ page.meta }}{% endif %}</p>\n  </header>\n  <p class="post-p">{{ content }}</p>\n  <footer class="post-footer">\n    <p class="rss-subscribe">subscribe <a href="{{ "/feed.xml" | prepend: site.baseurl }}">via RSS</a></p>\n  </footer>\n</article>\n
Run Code Online (Sandbox Code Playgroud)\n\n

但它很简单不起作用,而且都没有返回错误。\n在 Jekyll 中可以更改_config.ymlpost.html模板布局中设置的变量的值吗?

\n

Dav*_*uel 1

看来您无法重新分配site.variable值。如果设置{% assign site.showheader = false %}为 则不起作用。showheader: true_config.yml

如果您想根据您所在的页面显示标题,只需在页面前面设置变量即可。

在index.html中:

---
showheader: true
layout: default
---
Run Code Online (Sandbox Code Playgroud)

在帖子中:

---
showheader: false
layout: default
---
Run Code Online (Sandbox Code Playgroud)

在 _layouts/default.html 中

{% if page.showheader == true %}
    header
{% endif %}
Run Code Online (Sandbox Code Playgroud)