How to access front matter default values in static files


#1

How do I access front matter default values in static files? I’m trying to list all images in my site (in my /images folder) but am having trouble.

With the 3.4.2 release, you can now add defaults for static files. I upgraded to 3.4.2.

In my config file, I added this default:

  -
    scope:
      path: "images/"
    values:
      image: true

I have an images folder containing a bunch of image files. With this default, each image should have image: true in its front matter.

I want to create a list of all of these images (to fulfill reqs in a manifest for something). Here’s what I tried:

{% for file in site.static_files %}
{% if file.image == true %}
	{{file.path}} 
{% endif %}
{% endfor %}

It didn’t work.

I’m just not sure how to access the page front matter. Would it be file.image, page.image? I tried various things but none worked.

This is what I ended up doing that did work:

{% for file in site.static_files %}
{% if file.extname == ".png" or file.extname == ".jpg" or file.extname == ".gif" %}
	{{file.path}} 
{% endif %}
{% endfor %}
```

The problem is that, besides not leveraging the front matter values, the output ends up having lots of white space. It seems with each iteration through the loop, if the condition isn't met, a line break is still created. I was hoping that the front matter approach would eliminate all of that white space.

#2

I got it to work, looks like the path part is picky. Try it without any slash - “images” in the scope.

I’m on windows, maybe that is why the example in the docs didn’t work for me?

I have images in an assets directory,

  • “assets/” did nothing
  • “/assets” did nothing
  • “assets” worked
  • “assets/images” worked

Also, I find that you can eliminate a lot of white space in for/if loops by combining all into one line.


#3

Thanks for replying. I tried removing / from the images path in the config (then restarted jekyll), but it didn’t seem to do anything. You used file.<front matter property> to access the default front matter property set on those static files?


#4

I copied and pasted your example and it worked just as you have it. file.image works since it is in a for loop and you called it for file. Pretty sure you know that bit though.

In the config I have:

defaults:
   -
    scope:
      path: "assets/js"
    values:
      image: true

the above example filtered it out to just my one js file but I also did path: "assets" which gave me everything in assets.

Also, maybe make sure you are really on 3.4.2? I played with it for a good 30 minutes before realizing I hadn’t upgraded! jekyll --v


#5

another thing to try to trouble shoot it - try path: "" and that should add the image: true bit to everything, can help eliminate the path wonkyness.


#6

The following worked as expected:

  • add 2 image files to <source>/assets/img/
  • add 1 image file to <minima-gem>/assets/img/
# _config.yml

defaults:
  - scope:
      path: "assets/img"
    values:
      image: true
<!-- test.md -->

<div class="static_images">
  {% for file in site.static_files %}
    {% if file.image == true %}
      {{ file.path }}
    {% endif %}
  {% endfor %}
</div>
<!-- end .static_images -->
$ bundle exec jekyll build
<!-- _site/test.html -->

<div class="static_images">
  /assets/img/img_at_source.png
  /assets/img/img_at_source_2.png
  assets/img/img_in_minima.jpg
</div>
<!-- end .static_images -->

#7

Thanks. You’re right – that does work. I just tested it out in a new jekyll site. I’m not sure what I was doing wrong before. I appreciate your help.