【问题标题】:Passing filename as variable to Jekyll include doesn't work将文件名作为变量传递给 Jekyll 包含不起作用
【发布时间】:2018-04-06 03:16:26
【问题描述】:

这很好用:

{% capture foo %}{% include test.html %}{% endcapture %}

我想这样做:

frontmatter.md:

---
baaz: test.html
layout: layout.html
---    

layout.html:

{% capture foo %}{% include {{ page.baaz }} %}{% endcapture %}

但是当我这样做时,我得到了这个错误:

“液体异常:包含标记的语法无效。文件包含无效字符或序列:有效语法:{% include file.ext param='value' param2='value' %}”

我已经在其他几个问题中看到了这个问题,most recent explanation 我发现是这样的:

"...由于包含的文件是在编译阶段而不是在运行阶段计算和添加的,因此无法添加动态文件名路径。编译阶段意味着动态路径尚未被识别。 "

但是那个来源已经有将近两年的历史了。有人对此有解决方案吗?或者一种允许我在 frontmatter 中包含定义为变量的文件的解决方法?

【问题讨论】:

    标签: include jekyll liquid


    【解决方案1】:

    你可以试试{% include page.baaz %}

    编辑:经过一些调查,您的语法似乎是正确的,并且仅当page.baaz 不存在时才会触发错误。

    这最终出现在一个包含标签中,对于液体来说看起来像这样:

    {% include %}
    

    为了避免在未设置baaz 的某些页面/帖子上出现此错误,您可以使用条件。

    {% if page.baaz %}
      {% capture foo %}{% include {{ page.baaz }} %}{% endcapture %}
    {% endif %}
    

    【讨论】:

    • 你可以随时尝试... ;-)
    • .. 并且必须始终尝试!
    • 我确实试过这个,对我来说它一直在产生“在任何 ['/_includes'] 中找不到'page.baaz'...
    【解决方案2】:

    我最近才来这个案子。我假设语法works as expected。请参阅sampleresult

       {% include {{ page.baaz }} %}
    

    但是,在您的情况下,它可能是变量中的 page name could not be put,如错误所述:

    Error:  Invalid syntax for include tag:
    
      File contains invalid characters or sequences
    
    Valid syntax:
    
      ***% include file.ext param='value' param2='value' %***
    

    所以为了解决这个问题,我建议你清点所有文件名和choose它:

    {% case page.baaz %}
    {% when 'test.html' %}
        {% capture foo %}{% include test.html %}{% endcapture %}
    {% when 'othertest.html' %}
        {% capture foo %}{% include othertest.html %}{% endcapture %}
    {% else %}
        This is not a test
    {% endcase %}
    

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题...我找到了一个非常有用的解决方法。请允许我分享我的经验和解决方案。我希望它可以帮助您找到适合您的问题的解决方案。

       

      我想要构建的东西
      我想制作一个包含多个部分的页面。这些部分应该是可重复使用的,能够包含包含,并且应该易于在 CloudCannon CMS 中管理。


      我想出了什么
      我最终使用了以下前题:

      ---
      title: Lorem ipsum
      description: Lorem ipsum
      image: /img/default.jpg
      section_blocks:
        - section: sectionwithinclude
        - section: anothersection
        - section: andyetanothersection
      ---
      

      ... 和以下模板:

      {% for item in page.section_blocks %}
      {% for section in site.sections %}
          {% if item.section == section.slug %}
              <div class="section {{ item.section }}">
                  {{ section.content }}
              </div>
          {% endif %}
      {% endfor %}
      {% endfor %}
      

      _sections 文件夹/集合中,我有一个名为sectionwithinclude.md 的文件,如下所示:

      ---
      ---
      
      {% include mycustominclude.html %}
      

      为什么这很棒
      当您编辑页面时,CloudCannon 会将 section_blocks 显示为带有重新排序按钮的数组。此外,CloudCannon 将自动将section 识别为集合并在下拉列表中显示选项。因此,添加一个部分就是向数组中添加一个空项,从下拉列表中选择一个部分并使用数组按钮对其进行重新排序。同时,CloudCannon 的内联编辑选项仍然有效。所以文本的管理可以是所见即所得的,而块的管理可以在前端数组中完成。

      对(您和)您的编辑来说超级简单且功能强大。

       

      PS。您可能会发现您将遇到一些“范围”问题,因为page 不再与实际页面相关,而是与该部分相关。要解决此问题,您可以/应该更改模板中的循环。您可以让循环管理包含而不是部分。

      【讨论】:

        猜你喜欢
        • 2016-03-31
        • 2023-01-08
        • 1970-01-01
        • 2019-07-31
        • 2010-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-16
        相关资源
        最近更新 更多