Jekyll:参数(部分?)包括

时间:2014-04-24 08:45:08

标签: ruby templates jekyll partials

在Jekyll中,我想在输出页面中包含一段HTML代码(创建一个引导程序,多个选项卡结构)。由于每个标签的内容都很长,我想继续在markdown中编写它,并支持命名链接。

但是,我有不同页面的不同数量的标签,所以我想将捕获的内容作为include语句的参数传递。例如,我希望我的.md文件类似于:

---
layout: default
title: Test 
keywords: Test 
description: Test
datecreated: 24 Apr 2014
---

# Test Page

{% capture content1 %}
Some content here.

I want to be able to render **markdown markup**, like _this_.
{% endcapture %}

{% capture content2 %}
Some more content.

I want to be able to have [links][google] as well.

[google]: http://www.google.it/
{% endcapture %}

{% include myparametrictemplate.html sections="content1,content2" %}

到目前为止,我创建了以下myparametrictemplate.html

## This is a test, to confirm that "content1" and "content2" are accessible

### Section1

<div class="mytab">
{{ content1 | markdownify }}
</div>

### Section2

<div class="mytab">
{{ content2 | markdownify }}
</div>

<hr/>


## Here is the part I would like to have working

Check that `include.sections = "content1,content2"`: {{ include.sections }}

{% assign arr = include.sections | split:"," %}

{% for item in arr %}

Here I got printed the string "content1" (or "content2"), not the actual content of that Liquid tag:

<div class="mytab">
{{ item | markdownify }}
</div>

{% endfor %}

<hr/>



## The ugly workaround

{% if content1 %}
### Section1

<div class="mytab">
{{ content1 | markdownify }}
</div>
{% endif %}


{% if content2 %}
### Section2

<div class="mytab">
{{ content2 | markdownify }}
</div>
{% endif %}


{% if content3 %}
### Section3

<div class="mytab">
{{ content3 | markdownify }}
</div>
{% endif %}

如您所见,{{ item }}打印了名称(上例中的content1content2),而我想输出content1的内容或content2。后者是否可以某种方式实现?我无法在Liquid文档中找到任何内容。

示例的最后一部分显示了我设计的丑陋的解决方法,我对此并不满意(因为我不能在同一页面上有多个包含---每个包含一组不同的标签)。

编辑:稍微不那么丑陋的解决方法(但仍然不优雅)如下:

## The slightly less ugly workaround

{% assign arr = include.sections | split:"," %}

{% if arr contains "content1" %}
### Section1

<div class="mytab">
{{ content1 | markdownify }}
</div>
{% endif %}


{% if arr contains "content2" %}
### Section2

<div class="mytab">
{{ content2 | markdownify }}
</div>
{% endif %}


{% if arr contains "content3" %}
### Section3

<div class="mytab">
{{ content3 | markdownify }}
</div>
{% endif %}

我承认我是Jekyll的新手,因此我可能会走错路来解决我的问题。感谢任何指示。

注意:我想避免在https://stackoverflow.com/a/14247353/2452440(或http://wolfslittlestore.be/2013/10/rendering-markdown-in-jekyll/)中提出的解决方案,因为我希望将内容部分保留在同一页.md文件中。

1 个答案:

答案 0 :(得分:0)

一种简单的方法是:

# myparametrictemplate.html

<div class="mytab">
{{ content1 | markdownify }}
</div>

<div class="mytab">
{{ content2 | markdownify }}
</div>

<div class="mytab">
{{ content3 | markdownify }}
</div>

您不需要传递参数以包含,因为包含的文件将继承包含文件的范围。

所以你可以打电话:

{% include myparametrictemplate.html %}

遗憾的是,无法在液体模板中传递或创建新数组。因此,您无法循环浏览内容&#34;。