The Oddler
The Oddler

Reputation: 6698

Jekyll: How to include specific file from collection?

I have a collection of css files I combine into one with Jekyll. This is very easy:

{% for file in site.css %}
{{ file }}
{% endfor %}

However, there is a special case where I want to include just a single one of those files. The pages.css file.

site.css is a collection, so I simply put all my css files in a folder called _css and in _config.yml added:

collections:
  css:
    output: false

I was able to include only the _css/pages.css files using {{ site.css | where:"title", "Pages" | first }}. But I was wondering if there is a nicer way of doing it. Something like {{ site.css/pages.css }}

Is there a way to do it like that? Wihtout a where and first clause?

Upvotes: 1

Views: 377

Answers (1)

Mr. Hugo
Mr. Hugo

Reputation: 12582

For the exception I would use an if statement and not loop over the files.

However, I would consider cache while using this solution. If one pages uses 'superlargecombined.css' and another one uses 'superlargecombinedcsswithoutonepart.css' the client needs to download them both (fully). That is in not an optimization. If you keep your css files seperated they can be cached and will be downloaded once, independent of your page css configuration. Combining things that always need to stick together is a good solution though (to minimize requests), but note that with HTTP2 the need to minimize requests will reduce drastically.

Hope this helps!

Upvotes: 2

Related Questions