Skip to content

Instantly share code, notes, and snippets.

@sskylar
Last active November 24, 2017 17:26
Show Gist options
  • Save sskylar/0b07f5f96c276c9913db60372246f8b2 to your computer and use it in GitHub Desktop.
Save sskylar/0b07f5f96c276c9913db60372246f8b2 to your computer and use it in GitHub Desktop.
Loop through Uploads in Siteleaf (or any files in a Jekyll collection)
---
---
{% assign collection = site.collections | where: 'label', 'uploads' | first %}
{% for file in collection.files %}
<li><img src="/uploads/{{ file.name }}">
{% endfor %}
@sskylar
Copy link
Author

sskylar commented Nov 20, 2017

Your _config.yml should look something like this:

collections:
  uploads:
    output: true

@letrastudio
Copy link

letrastudio commented Nov 23, 2017

Might be helpful: Jekyll gives us direct access to file extensions through file.extname, so we can handle different file types. It's not unusual for my Uploads to end up containing other stuff like videos or PDFs.

I guess this is a sort of pull request:

---
---
{% assign collection = site.collections | where: 'label', 'uploads' | first %}
{% for file in collection.files %}

  {% assign extension = file.extname | downcase %}
  {% case extension %}

    {% when ".jpg" or ".jpeg" or ".png" or ".gif" or ".svg" %}
    <li><img src="/uploads/{{ file.name }}"></li>

    {% when ".mp4" %}
    <li><video controls src="/uploads/{{ file.name }}"></video></li>

    {% else %}
    <li><a href="/uploads/{{ file.name }}">{{ file.name }}</a></li>
    
  {% endcase %}

{% endfor %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment