2

Maybe this is a case of a Python programmer trying to work with Ruby and maybe this is a "feature" -- I don't know. For the life me I can't figure out how to set state on posts during the rendering process.

I have a _layout that just calls include twice:

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

templateA walks the posts and renders some of them on the basis of some_condition.

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

templateB attempts to walk the posts and render the rest:

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

This does not work as expected. I have also tried the {% assign post[rendered] = true %} syntax. No errors are thrown; just silent failure.

Where am I failing here? Is my mental model for the rendering process just totally wrong? Thanks!

2
  • I'm don't know jekyll's internals. Perhaps the include sare not run sequencitally, or variable assignment doesn't propagate. How about checking the some_condition again inside templateB?
    – Mehdi
    Jan 15, 2016 at 15:59
  • 1
    Yeah, that's the backup plan. It's poor encapsulation, though. Jan 15, 2016 at 16:14

1 Answer 1

0

Your first though was nearly ok. The unless tag does the contrary of if. But you cannot change jekyll's variables, they are "freezed".

So, this might work :

templateA

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

With the same condition unless will render posts not rendered by the first loop.

templateB

{% for post in site.posts %}
  {% unless same_condition %}
    <!-- Render the post -->
  {% endunless %}
{% endfor %}
3
  • Could you cite a source in the docs that clearly indicates that the post attributes are "frozen?" I can't find the scoping rules outlined anywhere. Jan 15, 2016 at 17:03
  • @BrianTheLion You'll find nothing in the doc about this. It comes from my experience : using Jekyll and reading source code. Jan 16, 2016 at 18:39
  • The only thing I've found is a comment from a Shopify Liquid dev here. You can also read the code like explained here. Jan 21, 2016 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.