Which folder should I put my static files in Jekyll?

*爱你&永不变心* 提交于 2021-02-07 05:33:27

问题


While I am looking the doc. I saw the following document structure,

.
├── _config.yml
├── _drafts
|   ├── begin-with-the-crazy-ideas.textile
|   └── on-simplicity-in-technology.markdown
├── _includes
|   ├── footer.html
|   └── header.html
├── _layouts
|   ├── default.html
|   └── post.html
├── _posts
|   ├── 2007-10-29-why-every-programmer-should-play-nethack.textile
|   └── 2009-04-26-barcamp-boston-4-roundup.textile
├── _data
|   └── members.yml
├── _site
├── .jekyll-metadata
└── index.html

When I need to include an image inside my post. Where should I put the image to use the feature of site.static_files mentioned here (Static Files Section in the Documentation)? So that I can use variable like file.path and file.modified_time directly.

Previously when I was using Jekyll 2.x, I was doing something like below by creating my own asset directory.

<link rel="stylesheet" href="{{ "/assets/css/index.css" | prepend: site.url }}">


回答1:


Assuming you have your gallery images in a structure like

-img
  -gallery
    -image1.png
    -image2.png
    etc.

you can access them in a collection or page like this:

{% for image in site.static_files %}
{% if image.path contains 'img/gallery' %}
    <p>{{image.path}} - {{image.modified_time}}</p>
    <img src="{{site.baseurl}}{{image.path}}">
{% endif %}
{% endfor %}

This goes through all static files and check for a certain path (img/gallery in this example).

Than you can access the static file metadata for that file. (I named it 'image' in this example but you can name it whatever you want after the for keyword).

I think it doesn't make too much sense to put something like this in a blog post but rather in a page or a collection.




回答2:


A static file can be placed anywhere within the site directory that is not a collection of some sort. So not within a directory that begins with "_". What makes it static is the fact that it does not have YAML frontmatter.

Id recommend making a directory named "assets" or "images" and have them in there.

edit:

what are you trying to use the feature for?



来源:https://stackoverflow.com/questions/36448853/which-folder-should-i-put-my-static-files-in-jekyll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!