【问题标题】:Jekyll (Liquid): Setting post state in included templatesJekyll (Liquid):在包含的模板中设置帖子状态
【发布时间】:2016-01-15 15:19:39
【问题描述】:

也许这是一个 Python 程序员尝试使用 Ruby 的案例,也许这是 "feature"——我不知道。对于我的生活,我无法弄清楚如何在渲染过程中设置帖子的状态。

我有一个_layout,它只调用了两次include

{% include templateA %}
{% include templateB %}

templateAsome_condition 的基础上遍历帖子并渲染一些

{% for post in site.posts %}
{% if some_condition %}
   <!-- Render the post -->
{% assign post.rendered = true %}
{% endif %}
{% endfor %}

templateB 尝试遍历帖子并渲染其余部分

{% for post in site.posts %}
{% unless post.rendered %}
   <!-- Render the post -->
{% endif %}
{% endfor %}

这没有按预期工作。我也尝试过{% assign post[rendered] = true %} 语法。没有错误被抛出;只是无声的失败。

我在哪里失败了?我对渲染过程的心理模型完全错误吗?谢谢!

【问题讨论】:

  • 我不知道 jekyll 的内部结构。也许包含不按顺序运行,或者变量赋值不传播。在 templateB 中再次检查 some_condition 怎么样?
  • 是的,这就是备用计划。不过,它的封装很差。

标签: jekyll liquid


【解决方案1】:

虽然你的第一个几乎没问题。 unless 标签与if 正好相反。 但是你不能改变 jekyll 的变量,它们被“冻结”了。

所以,这可能有效:

模板A

{% for post in site.posts %}
  {% if same_condition %}
    <!-- Render the post -->
  {% endif %}
{% endfor %}

在相同的条件下unless 将呈现第一个循环未呈现的帖子。

模板B

{% for post in site.posts %}
  {% unless same_condition %}
    <!-- Render the post -->
  {% endunless %}
{% endfor %}

【讨论】:

  • 您能否引用文档中明确表明帖子属性已“冻结”的来源?我在任何地方都找不到概述的范围规则。
  • @BrianTheLion 您在文档中找不到任何关于此的内容。它来自我的经验:使用 Jekyll 和阅读源代码。
  • 我发现的唯一内容是来自 Shopify Liquid 开发人员 here 的评论。你也可以阅读here解释的代码。
猜你喜欢
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 2012-04-21
  • 2015-03-24
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
相关资源
最近更新 更多