Jekyll: Include a file from directory outside of _

2020-05-31 05:45发布

问题:

I have an directory called /patterns in my Jekyll site, whose structure generally looks generally like this:

_includes _layouts _site /patterns index.html

I need to keep the /patterns directory outside _includes for a number of reasons, but mostly because I need to pull the files in /patterns into an iframe to display in a pattern library).

I'd like to include files from /patterns in my Jekyll pages, but using {% include /patterns/file.html %} doesn't work as it points to the_includesfolder. How would I go about including a file from a directory that isn't_includes`?

回答1:

You can change the directory that the include tag uses with includes_dir in your _config.yml. It doesn't look like you can set multiple paths (source: https://jekyllrb.com/docs/configuration/).

In any case, the files in _includes don't end up in your output. You could separate pattern-specific includes into _includes/patterns/, but the only thing that'd have any effect on your live site would be where those files were included.



回答2:

I think that collections will do what you need. They can be included and rendered as public html.

Here's an example project that does just this.



回答3:

You can choose to include file fragments relative to the current file by using the include_relative tag for your /patterns/file.html

For the directory structure you have:

_includes
_layouts
_site
/patterns/file.html
index.html

In this case the following doesn't work:

{% include /patterns/file.html %}

Use include_relative as /pattern is relatif to index.html as the current file:

{% include_relative patterns/file.html %}

Note:

You can't use the include_relative tag with any files inside your layouts folder. You can only use include_relative on a page or post. Layouts code is executed in the context of page/post rendering and the include_relative is calculated relative to this page or post, not from the layout itself.

In this case your code on index.html shall be:

---
layout: null
---
(put all code from _layouts/default.html)
{% include_relative patterns/file.html %}


回答4:

In your _config.yml, you can add additional directories like so:

includes:
- patterns

Just as simple as that!

In action here on my Jekyll site: https://github.com/pschfr/pschfr.github.io/blob/master/_config.yml



回答5:

I put a symlink in place of the _includes directory that points where I want, since includes_dir doesn't seem to like ../ to specify a relative (one or more directories higher) path.



回答6:

The ONLY solution I have found for this is to use this plugin

https://github.com/tnhu/jekyll-include-absolute-plugin