1
votes

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.

2

2 Answers

1
votes

You can't mix tags and filters (except for assign). Moreover, there's no need to enclose variables to a filter within double braces:

---
directory_to_scan: '/comics/'
---

And the layout would use:

{% assign my_pages = site.pages | where: 'dir', page.directory_to_scan %}
{% for p in my_pages %}
  {{ p.title }}
{% endfor %}
0
votes

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