Passing parameters to inclusion in Liquid for loop - jekyll

have trouble with passing
{% include /components/home-lastest-posts.html nb=8 %}
code in my template home-lastest-posts.html :
{% for post in site.posts offset:0 limit:{{ include.nb }} %}
do something
{% endfor %}
It's listing my posts blog but my param not working. What is wrong ?
have try nb="8" too but not working too

Simply use : {% for post in site.posts offset:0 limit:include.nb %}.

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.

Jekyll -- possible to cycle through all post types?

Is it possible to cycle through all content types on Jekyll?
Instead of {% for post in site.pages %}
I want to do something like
{% for post in site.pages or site.posts %}
You can use concat filter :
{% assign all = site.posts | concat: site.pages %}
{% for post in all %}
...

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

Jekyll (Liquid): Setting post state in included templates

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

Getting the related posts from specific category (category/_posts)

I am doing the following
{% for post in site.related_posts limit:3 %}
// My stuff
{% endfor %}
and it keeps returning the posts from each of my categories i.e. blog/_posts/ as well as projects/_posts/ what I want is only blog posts. So I tried to do the following
{% for post in site.categories.blog.related_posts limit:3 %}
// My stuff
{% endfor %}
But it doesn't seem to return anything at all. Can any one please tell me what am I doing wrong here?
I never use related_posts, but this fairly ugly bit might work for you:
capture the category
{% capture related_posts %}{{ page.categories }}{% endcapture %}
Use that category to create your related posts
{% assign collection = site.categories[related_posts] %}
Return the related posts in a loop, pulling out the current page's post
{% for article in collection %}
{% unless page.url == article.url %}
{{ article.title }} - {{ category | capitalize }}<br />
{% endunless %}
{% endfor %}
Both this and related posts are processor intensive if you have a lot of posts.