Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll array contains check

I've an array in my _config.yaml. Let's say

exclude_pages: [ "/404.html", "/search.html", "/atom.xml", "/rss.xml", "/index.html", "/sitemap.txt" ]

What I want to do is exclude these pages in the pages loop of site.pages. So following is the code that I'm trying.

{% for entry in site.pages %}
    {% if site.exclude_pages contains entry.url %}
        <!-- Do Nothing -->
    {% else %}
        <!-- Show Page -->
    {% endif %}
{% endfor %}

But somehow it is not happening. All the pages are being ignored in this code.

Any idea what I'm missing here ?

like image 271
Udit Desai Avatar asked Nov 21 '14 15:11

Udit Desai


2 Answers

Try :

exclude_pages: [ "index.html", "anyfolder/index.html" ]

Then loop with entry.path not entry.url:

{% for entry in site.pages %}
    {% if site.exclude_pages contains entry.path %}
        <!-- Do Nothing -->
    {% else %}
        <!-- Show Page -->
    {% endif %}
{% endfor %}
like image 195
David Jacquel Avatar answered Nov 20 '22 06:11

David Jacquel


According to template docs you my try to use Where Expression with contains and unless.

{{ assign entries = site.pages | where_exp:"item", "unless item.url contains site.exclude_pages" }}
{% for entry in entries %}
   <!-- Show Page -->
{% endfor %}

See sample how a live code works to generate feed.json in my github site.

like image 1
Chetabahana Avatar answered Nov 20 '22 08:11

Chetabahana