michaelx / jekyll_file_exists

A Jekyll plugin that makes it easy to check for the existence of a file.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jekyll File Exists

A Jekyll plugin that makes it easily possible to check the existence of a file.

Installation

Copy file_exists.rb into the /_plugins/ directory of your Jekyll project.

Usage

Basic Example

The plugin adds a new custom liquid tag, which can be used as follows:
{% file_exists images/file-example.jpg %}

The output will be true or false.

You can also use liquid objects within the tag:
{% file_exists {{ author_photo_url }} %}

Important: The path needs to start at the root level of your Jekyll project and without a leading slash /.

Production Example

I’m using the plugin on My Morning Routine to check if an author image exists, otherwise Jekyll should use our placeholder image.

1. Check if the file exists AND capture the result

We’re saving the plugins output to author_photo:
{% capture author_photo %}{% file_exists {{ author_photo_url }} %}{% endcapture %}

2. Write an if/else clause for the result

If the author photo exists true we gonna use it, otherwise false we use the placeholder image. You need to put quotes around "true" as the return value to Liquid is a string.

{% if author_photo == "true" %}
  {% assign author_photo = author_photo_url | prepend: "/" | prepend: site.baseurl %}
{% else %}
  {% assign author_photo = "no-photo.jpg" | prepend: "/images/routines/" | prepend: site.baseurl %}
{% endif %}

Credits

About

A Jekyll plugin that makes it easy to check for the existence of a file.

License:MIT License


Languages

Language:Ruby 100.0%