Search code examples
htmljekyllliquid

Liquid filtering array over a page property


I've got a jekyll site with two pages (page1.html and page2.html), they both use the same layout. This layout print some information about some other pages in a given subdirectory. For example

  • /_layouts/test.html
{% for p in site.pages %}
    {{ p.title }}
{% endfor %}
  • /page1.html
---
layout: test
---
this page lists the title of my books...

This would print the title of every page in my site, but I want it to print only the title of pages in the subdirectory /books, so I would change the layout page to

{% for p in site.pages | where: 'dir','/books/' %}
    {{ p.title }}
{% endfor %}

This works fine, but I would like another page to use the same layout and list the content of my comics (that are inside /comics folder) instead of my books, so I would change the structure of my site in the following way:

  • /_layouts/test.html
{% for p in site.pages | where: 'dir','/{{ page.directory_to_scan }}/' %}
    {{ p.title }}
{% endfor %}
  • /page1.html
---
layout: test
directory_to_scan: books
---
this page lists the title of my books...
  • /page2.html
---
layout: test
directory_to_scan: comics
---
this page lists the title of my comics...

However this does not work and no title at all is printed.


Solution

  • So, I've eventually solved by assembling the string before the loop, using the append filter.

    {% assign d = '/' | append: page.directory_to_scan | append: '/' %}
    {% for p in site.pages | where: 'dir',d %}
        {{ p.title }}
    {% endfor %}