Jekyll: Include a file from directory outside of _includes

后端 未结 7 1524
陌清茗
陌清茗 2021-02-07 03:34

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

_includes _layouts _site /patterns index

相关标签:
7条回答
  • 2021-02-07 03:40

    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.

    0 讨论(0)
  • 2021-02-07 03:42

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

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

    0 讨论(0)
  • 2021-02-07 03:43

    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.

    0 讨论(0)
  • 2021-02-07 03:45

    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

    0 讨论(0)
  • 2021-02-07 03:46

    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.

    0 讨论(0)
  • 2021-02-07 04:00

    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 %}
    
    0 讨论(0)
提交回复
热议问题