Jekyll:在_includes之外的目录中包含一个文件

Bra*_*ost 18 include jekyll

我有一个/patterns在我的Jekyll网站中调用的目录,其结构通常看起来像这样:

_includes _layouts _site /patterns index.html

我需要将/patterns目录保留在外面_includes有很多原因,但主要是因为我需要将文件拉/patterns入iframe以显示在模式库中).

我想/patterns在我的Jekyll页面中包含文件,但是使用{% include /patterns/file.html %} doesn't work as it points to the_includes folder. How would I go about including a file from a directory that isn't_includes`?

Che*_*ana 10

您可以选择使用您的标签 来包含与当前文件相关的文件片段。include_relative/patterns/file.html

在此输入图像描述

对于目录结构,您有:

_includes
_layouts
_site
/patterns/file.html
index.html
Run Code Online (Sandbox Code Playgroud)

在这种情况下,以下内容不起作用:

{% include /patterns/file.html %}
Run Code Online (Sandbox Code Playgroud)

使用include_relativeas /pattern is relatif toindex.html作为current file

{% include_relative patterns/file.html %}
Run Code Online (Sandbox Code Playgroud)

笔记

您不能将该include_relative标签与布局文件夹内的任何文件一起使用。您只能include_relative在页面或帖子上使用。布局代码在页面/帖子渲染的上下文中执行,并且 include_relative 是相对于该页面或帖子计算的,而不是根据布局本身计算的

在这种情况下,您的代码index.html应为:

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


Ros*_*oss 7

您可以更改目录include标签与使用includes_dir在你的_config.yml.看起来您可以设置多个路径(来源:https://jekyllrb.com/docs/configuration/).

在任何情况下,文件_includes都不会在输出中结束.您可以将特定于模式的包含分隔为_includes/patterns/,但对您的实时网站有任何影响的唯一因素是包含这些文件的位置.


Pau*_*aul -1

在 _config.yml 中,您可以添加其他目录,如下所示:

includes:
- patterns
Run Code Online (Sandbox Code Playgroud)

就这么简单!

在我的 Jekyll 网站上进行操作:https://github.com/pschfr/pschfr.github.io/blob/master/_config.yml

  • `includes` 设置实际上称为 `include`,并不为 `{% include %}` 标签设置额外的文件夹。它在输出中包含其他排除的文件/文件夹。请参阅 https://jekyllrb.com/docs/configuration/ (10认同)
  • 伟大的!如果我设置该选项,“/patterns”是否仍会编译并显示在“/_site”中? (2认同)
  • @pschfr 你能显示你从网站上的 _includes 目录之外调用包含的位置吗?我想看看你是如何做到这一点的一个例子,但看不到。谢谢。 (2认同)