Reputation: 392
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
{% for p in site.pages %}
{{ p.title }}
{% endfor %}
---
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:
{% for p in site.pages | where: 'dir','/{{ page.directory_to_scan }}/' %}
{{ p.title }}
{% endfor %}
---
layout: test
directory_to_scan: books
---
this page lists the title of my books...
---
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.
Upvotes: 1
Views: 512
Reputation: 392
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 %}
Upvotes: 0
Reputation: 5444
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 %}
Upvotes: 1