Jekyll (Liquid): Setting post state in included templates - jekyll

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!

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 %}

Related

Jekyll/Liquid Relative URL filter breaks links

I'm trying to use relative_url in most of the links of my Jekyll theme, so if someone wants to have this theme working in a subdirectory he can do it.
I have a problem with the list of categories of the post, each of which should link to the archive.
In _layouts/post.html I have this code:
{% if site.data.settings.categories.active %}
{% include categories.html %}
{% endif %}
categories.html has this code:
<div class="categories">
<span><p>Categories:</p>
{% if post %}
{% assign categories = post.categories %}
{% else %}
{% assign categories = page.categories %}
{% endif %}
{% for category in categories %}
{{category}}
{% unless forloop.last %} {% endunless %}
{% endfor %}
</span>
</div>
Here's the problem:
{{category}}
Somehow, this returns the current post url.
{{category}}
This returns the correct link, but does not work in case the site is in a subdirectory.
Why it returns the post url?
There are multiple problems here.
First off, Liquid doesn't evaluate nested constructs.
Therefore, the following code:
{{ "/categories/#{{category | slugify}}" | relative_url}}
needs to be rewritten into:
{% capture url %}/categories/{{ category | slugify }}{% endcapture %}
{{ url | relative_url }}
Secondly, there is no global post object. Therefore {% if post %} is always going to evaluate to a negative. i.e., it is redundant.

how to displayliquid code in Jeckyll post

Hello I am trying to make a post using jeckyll and as part of my post I would like to show some liquid code. The post should display the IF statement as part of the post text (example below).
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
I have tried to post this as
{% highlight liquid %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
and also
{% highlight markdown %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
but anything I try seems to be still executing the liquid code.
Is there a way to display the IF statement my post?
Try wrapping your liquid code in {% raw %} {% endraw %} like this:
{% highlight liquid %}
{% raw %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endraw %}
{% endhighlight %}
The raw tag will disable any liquid processing and output your code as desired.

Cannot load multiple commenting system in Jekyll blog

I am trying to implement disqus and facebook commenting system for different categories in jekyll blog.
Here is my current approach.
{% for category in site.categories %}
{% if category.type == "personal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
{% endfor %}
Expected result: Facebook comment should be loaded in category personal from facebook.html otherwise disqus comment should be loaded in all other category.
Actual result: Disqus comments loads automatically in personal category regardless of the loop.
What should be changed to make the comments load correctly?
It seems that you want to print disqus or facebook on each page depending on "personnal" category presence.
As #marcanuy says, you have to refer to page.categories, which is an array.
{% if page.categories contains "personnal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
Category doesn't have a type attribute. Checking directly like:
{% for category in page.categories %}
{% if category == "personal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
{% endfor %}
Should detect the personal category and load disqus comments.

Include the whole post, not only summary. Jekyll

I have a very noob question. I want to create a page, where will be presented all posts from some category. I understand, that I should write some {% include %} tag, but I could not understand which.
I have:
{% for post in site.posts %}
<!-- check if post comes from one of page categories-->
{% assign PostAndPageCategories= page.categories | concat: post.categories %}
{% capture all_cat %}{{ PostAndPageCategories | join: "," }}{% endcapture %}
{% capture unique_cat %}{{ PostAndPageCategories |uniq | join: "," }}{% endcapture %}
{% if all_cat != unique_cat %}
<!-- END check if post comes from one of page categories-->
{% include archive-single.html %}
{% endif %}
{% endfor %}
Obviously, this code shows me posts with teasers, but I need to display the whole post.
How should I change line {% include archive-single.html %}?
I found {{post.content}} instead of {% include archive-single.html %} work fine for me.

How to check if a value is present in a list?

I would like to get the list of posts having a specific tag in their front matter (*)
I tried the code below, which iterates over all the tags of the current post pages (the current tag is t), then iterates over all the posts (p) to check if they have this tag (and just outputs the title, for debugging reasons):
{% for t in page.tags %}
{% for p in site.posts %}
{% if t in p.tags %}
{{ p.title }}
{% endif %}
{% endfor %}
{% endfor %}
The line {% if t in p.tags %} seems to fail (I come form a Python background so I gave it a try) and I cannot find the in operator in liquid. Does it exits?
(*) I am mentioning what I want to achieve in case there is a more straightforward way to do that but I am still interested in the general question.
Following your example it can be done with the contains tag:
contains can also check for the presence of a string in an array of
strings.
{% if product.tags contains "outdoor" %}
This product is great for
using outdoors!
{% endif %}
So to get the list of posts having a specific tag in their front matter (in this case the posts with the tag mytag):
{% assign posts_with_mytag = site.posts | where_exp:"item",
"item.tags contains 'mytag'" %}
{% for post in posts_with_mytag %}
{{post.title}}
{% endfor %}
Following-up on #maracuny's answer, the corrected code from my question for completeness:
{% for t in page.tags %}
{% for p in site.posts %}
{% if p.tags contains t %}
{{ p.title }}
{% endif %}
{% endfor %}
{% endfor %}