【问题标题】:Include a file, but only if it exists?包含一个文件,但前提是它存在?
【发布时间】:2016-01-26 19:14:57
【问题描述】:

我正在 Jekyll 中创建一个样式指南,并使用 Collections 来定义指南的不同元素。比如headingslists等。

我正在尝试将 Sass 分成与部分匹配的文件,一对一,并且我想将 Sass 文件呈现为每个集合的一部分。

所以,类似:

{% if _includes/_sass/{{ entry.title | append: ".scss"}} %}
{% highlight sass %}
{% include _includes/_sass/{{ entry.title | append: ".scss" }} %}
{% endhighlight %}
{% endif %}

基本上,我想要的是“在此目录中包含一个与我的集合中的此条目同名的文件。如果它不存在,请不要破坏。”

我该怎么做?我已经探索过将文件路径存储在变量中,但似乎无法让它工作。

提前致谢。

【问题讨论】:

    标签: jekyll liquid github-pages


    【解决方案1】:

    可以做到的。

    这适用于 Jekyll 3,但它当然可以移植到 Jekyll 2。 从基础安装开始 (jekyll new)

    _config.yml

    collections:
      guide:
      sasssamples:
    

    样式指南文件

    • 我们的样本将归入_guide 集合。

    示例文件:_guide/header/header1.hmtl

    ---
    title: Header level 1
    ---
    <h1>Header level 1</h1>
    

    SCSS 示例

    • 我们希望我们的 SCSS 示例包含在我们的css/main.scss 中,并使用我们其他 SCSS 文件中定义的变量。我们的样品将整合在我们的末尾css/main.scss
    • 我们不希望我们的 SCSS 样本呈现为 css,因此没有 .scss 扩展名。切换到 .txt 扩展名
    • 我们希望从列表中访问 SCSS 示例。让我们将它们放入 sasssamples 集合中。

    示例文件:_sasssamples/header/header1.txt

    ---
    ---
    h1{
      color: $brand-color;
      border: 1px solid $brand-color;
    }
    

    SCSS 示例集成

    在引导 scss 文件的最后添加此代码(css/main.scss 在基本 Jekyll 安装上)

    css/main.scss

    [ original code ... ]
    
    {% comment %} Selecting a collection the Jekyll 3 way. See https://github.com/jekyll/jekyll/issues/4392 {% endcomment %}
    {% assign scssCollection = site.collections | where: 'label', 'sasssamples' | first %}
    {% comment %}
        Printing documents in sasssamples collection.
        All SCSS from style guide are sandboxed in .guide class
        This allows us to apply styles only to style guide html samples
    {% endcomment %}
    .guide{
        {% for doc in scssCollection.docs %}
            {{ doc.content }}
        {% endfor %}
    }
    

    风格指南

    <h2>Style guide</h2>
    {% comment %}Selecting a collection the Jekyll 3 way. See https://github.com/jekyll/jekyll/issues/4392 {% endcomment %}
    {% assign guideCollection = site.collections | where: 'label', 'guide' | first %}
    {% assign scssCollection = site.collections | where: 'label', 'sasssamples' | first %}
    
    {% comment %} Looping hover style guide samples {% endcomment %}
    {% assign samples = guideCollection.docs %}
    {% for sample in samples %}
        <article>
            <h3>Element : {{ sample.title }}</h3>
            <h4>Render</h4>
            <div class="guide">
                {{ sample.content }}
            </div>
            <h4>html code</h4>
            {% highlight html %}{{ sample.content }}{% endhighlight %}
            {% comment %}
            Changing a path like : _guide/headers/header1.html
            to :                   _sasssamples/headers/header1.txt
            {% endcomment %}
            {% assign scssPath = sample.path | replace: '_guide', '_sasssamples' %}
            {% assign scssPath = scssPath | replace: '.html', '.txt' %}
            {% comment %} Try to find a SCSS sample with equivalent path {% endcomment %}
            {% assign scssSample = scssCollection.docs | where: 'path', scssPath | first %}
            {% comment %}We print SCSS sample only if we found an equivalent path{% endcomment %}
            {% if scssSample != nil %}
                <h4>SCSS code</h4>
                {% highlight css %}{{ scssSample.content }}{% endhighlight %}
            {% endif %}
        </article>
    {% endfor %}
    

    完成!

    【讨论】:

    • 哇,戴夫,感谢您花时间捕捉所有这些!我几乎准备好了所有东西,包括一种包含 Sass 示例的方法,但我只需要一种方法来包含 Sass 示例,前提是它存在。
    • 它在if scssSample循环中。
    【解决方案2】:

    似乎只是错过了分配正确的路径

    {% if _includes/_sass/{{ entry.title | append: ".scss"}} 
    

    需要替换成scss文件的相对路径:

    {% assign scssPath = 'relative/path/to/your/scss/' %}
    {% if {{ entry.title | append: ".scss"  | prepend: scssPath }} != nil %}
    

    【讨论】:

      猜你喜欢
      • 2019-11-27
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多