包含 Jekyll/Liquid 代码而不渲染它

Include Jekyll/Liquid code without rendering it

是否可以 {% include file.html %} 不渲染其中的标签?

我正在寻找符合 {% include file.html | no_render %}

的内容

我无法将原始标签放入 file.html 的原因是我试图将其作为模板重新使用(这有点麻烦)。

这也适用于试图自我描述的页面。 IE。 This useful thing works like this: {% include useful_snippet.html | no_render %}

useful_snippet.html

{% raw %}
  {% comment %}Alot of liquid code{% endcomment %}
  {% assign toto = "Welcome to the hack !" %}
  {% assign string = "a,b,c,d" %}
  {% assign array = string | split:"," %}
  {% for item in array %}
    {{ item }}
  {% endfor %}
{% endraw %}

基地展示

<pre><code>{% include useful_snippet.html %}</code></pre>

使用 jekyll 高亮标签显示

{% highlight liquid %}{% include useful_snippet.html %}{% endhighlight %}

编辑: 在 Jekyll only process 中,Liquid 渲染一次。因此,没有办法让一个模板同时渲染 Liquid 和渲染原始 Liquid 代码。

如果你想这样做,你必须使用 generator plugin or hooks

对我来说,我没有在 {% raw %}{% endraw %} 中包装东西。 我的 js/html 组合被解析器略微修改,破坏了一些东西。

为了将 js/html 作为原始文件包含在内,我使用 kramdown's nomarkdown extension,如下所示:

{::nomarkdown}
<script type="text/javascript" src="https://www.pouet.net/pouet-player.js" async defer></script>
<div class="pouet-player" data-version="v1" data-size="medium"><a href='https://www.pouet.net/prod.php?which=30244'>fr-041: debris. by Farbrausch</a></div>
{:/}

这是一个带有自定义标签的解决方案:

raw_include_relative.rb 放入 Jekyll 根目录的 _plugins/ 目录中:

# _plugins/raw_include_relative.rb
module Jekyll
  module Tags
    class RawIncludeRelativeTag < IncludeRelativeTag
      # Overriding is explicitly allowed, to modify file content by subclassing:
      # https://github.com/jekyll/jekyll/blob/f5826eed3cde692f84e35140209d5a59ec3eb295/lib/jekyll/tags/include.rb#L178
      def read_file(file, context)
        # Hack: instead of including the file directly without liquid escaping,
        # simply wrap the entire file in a `raw` liquid tag, suppressing liquid
        # processing.
        "{% raw %}" + super + "{% endraw %}"
      end
    end
  end
end

Liquid::Template.register_tag("raw_include_relative", Jekyll::Tags::RawIncludeRelativeTag)

然后您可以像使用 include_relative 一样使用 {% raw_include_relative somefile.txt %}

In-practice example.