Search code examples
jekyllgithub-pages

How to access static files in a collection?


How do I access static files in a collection?

I have (I think) followed the instructions at

https://jekyllrb.com/docs/collections/

I created a dir ./_test and static files ./_test/a and ./_test/b and added a corresponding collection entry in _config.yml:

collections:
  - test

After this I cannot use site.test.files to get an array containing the files ./_test/a and ./_test/b (as should be possible, per my interpretation of the above Jekyll documentation).

(I use the Jekyll version provided by GitHub-Pages.)


Solution

  • Say you have both, some files with yaml front matter and static files witch are not processed by jekyll. These files can be accessed as follows:

    • files with front matter:

      {% assign test_docs = site.test %}
      {{ test_docs }}
      
    • static files:

      {% assign test_coll = site.collections | where: "label", "test" | first %}
      {{ test_coll.files }}
      

    As you asked explicitly for static files, in the code above {{ test_coll.files }} contains the array with both files /_test/a and /_test/b. But, only if these files do not have a yaml front matter.