jekyll: combine limit and where and reverse -


using jekyll i'd to:

  • iterate through pages
  • where page.path not current path
  • where page.categories contains "featured"
  • reverse them(most recent first)
  • limit 3

i'm having problems when getting filters together

{% assign posts = site.posts | reverse %} {% post in posts limit:3 %}   {% if post.categories contains 'featured' , post.path != page.path %}     {% include card.html post=post %}   {% endif %} {% endfor %} 

right limit not working because inner if prevent few items being rendered.

assign 0 counter variable before entering loop. don't set limit on loop, instead set condition on counter being below limit, , increment counter using liquid's plus filter every time meet criteria , output card.

{% assign posts = site.posts | reverse %} {% assign counter = 0 %} {% post in posts %}   {% if counter < 3 , post.categories contains 'featured' , post.path != page.path %}     {% include card.html post=post %}     {% assign counter = counter | plus: 1 %}   {% endif %} {% endfor %} 

a more complex example

i'm basing of on looping use myself. condition little more complex: check shared tag current page. i've included further example below.

{% assign counter = 0 %} {% post in site.posts %}     {% if counter < 4 %}         {% if post.url != page.url %}             {% assign isvalid = false %}             {% page_tag in page.tags %}                 {% post_tag in post.tags %}                     {% if post_tag == page_tag %}                         {% assign isvalid = true %}                     {% endif %}                 {% endfor %}             {% endfor %}             {% if isvalid %}                 {% include article_card.html %}                 {% assign counter = counter | plus: 1 %}             {% endif %}         {% endif %}     {% endif %} {% endfor %} 

why where fails

although liquid has where filter, can exact comparisons, have reinvent wheel in order achieve where-like scenario more complex conditions. make lot of looping through site.posts, jekyll being preprocessor means penalty of using inefficient improvised where-type looping compile-time concern, not runtime one. so, mitigate cost as possible, opt having counter condition first jekyll calculates.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -