So I needed to display a file if it exists. A quick search yielded two options:

  1. Some old, weird plugins on Github: 1, 2.
  2. Stackoverflow’s for loop of horror:
    {% for static_file in site.static_files %}
       {% if static_file.path == '/favicon.ico' %}
           {% assign favicon = true %}
       {% endif %}
    {% endfor %}
    

Both not very attractive, so I dived into docs to see if there’s something better. Here’s the result:

{% assign file = site.static_files | where: "path", jpg_path | first %}
{% if file %}<img src="{{ jpg_path }}">{% endif %}

No idea if it’s faster than the for loop, but at least it’s not as ugly.

Categories:

Updated:

Comments