Jekyll模板使用类似django的液体块/继承

xce*_*ion 11 ruby django inheritance liquid jekyll

我正在以一种很大的方式进入Jekyll,并希望将其用作一般的前端开发平台,但是我遇到了Liquid模板语言的局限性,特别是它与Django模板的区别.

我发现了液体继承宝石,它添加了Django中最重要的Extends和Block语法.此博客文章进一步扩展了宝石以适应Jekyll的文件系统:http: //www.sameratiani.com/2011/10/22/get-jekyll-working-with-liquid-inheritance.html

问题是它似乎没有以与Django完全相同的方式实现块,这实际上使得gem无用.

为了理解,我有两个jekyll"布局" - parent.html和child.html.这些都不包含YAML部分.

<html>
{% block foo %} {% endblock %}
</html>
Run Code Online (Sandbox Code Playgroud)

儿童

{% extends _layouts/parent.html %}
{% block foo %}
  <div>
    Bar comes next:
    {% block bar %} {% endblock %}
  </div>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

然后我有一个jekyll页面,其中包含一个YAML部分:

---
title: test
---

{% extends _layouts/child.html %}
{% block bar %}My title is {{ page.title }} {% endblock %}
Run Code Online (Sandbox Code Playgroud)

我期待的是:

<html>
  <div>
    Bar comes next:
    My title is test
  </div>
</html>
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

<html>
  <div>
    Bar comes next:
  </div>
</html>My title is test
Run Code Online (Sandbox Code Playgroud)

似乎有些事情未能将mypage.html中的块视为有资格插入父母/孩子的合适位置,尽管它显然仍在做些什么.

我不是一个红宝石开发人员,对Jekyll来说是个新手,所以我需要帮助确定这个堆栈的哪个部分失败了.github上的液体继承问题表明其他人正在遇到这种块嵌套问题:https://github.com/danwrong/liquid-inheritance/issues/3

我已经尝试了几种液体继承的分支,其中许多显然已经解决了问题正则表达式,但似乎没有解决这个问题.

我根本不可能做什么?看起来我至少有85%的方式,最后一点需要修复.

hel*_*ope 6

我不确定这是否会在杰基尔内部发挥作用.我可能错了,但这是我的理由:

https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb中使用do_layout渲染每个页面.

这是递归工作 - 它处理页面的内容,然后处理页面的布局,然后处理布局的布局等等,将YAML变量传递到链(因此它们总是在父模板中可用{{page .随你}}).

这意味着唯一被传递的东西是YAML值,以及在Liquid处理之后'content'的值.我不知道它在其他地方是如何完成的,但这似乎与块的概念不相容,因为它们需要你分别传递两个块.

从根本上说,在我看来,问题是Jekyll已经有了一种简单的继承形式 - 通过你可以给布局的"布局"属性.从根本上说,我认为这与液体模板兼容.

总而言之,我不确定你是否已经用尽了使用YAML,_includes和模板逻辑的限制.如果你正在将Django样式块放入你的内容中,为什么不做这样的事情:

内容:

---
title: some title
secondary_content: |
    Here is some *secondary* content that will be [markdownified](http://example.com).
    It can run to multiple lines and include
    * Lists
    * Good things
    * Etc
---

And here is the main content, as per usual
Run Code Online (Sandbox Code Playgroud)

模板:

<html>
<article>
    <h1>{{ page.title }}</h1>
    {{ content }}
</article>
<aside>
{{ page.secondary_content | markdownify}}
</aside>
Run Code Online (Sandbox Code Playgroud)

如果您希望保持模板清洁,并为不同类型的页面提供不同的内容,则可以使用各种包含:

模板:

<aside>
{% include sidebar_negotiation.html %}
</aside>
Run Code Online (Sandbox Code Playgroud)

_includes/sidebar_negotiation.html:

{% if page.type = 'foo' %}
{% include sidebar_foo.html %}
{% else if page.type = 'bar' %}
{% include sidebar_bar.html %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

然后将您的页面类型特定的东西放在这些文件中.显然你可以直接包含它,但它可能很好地将它抽象出来.这些包含将获得YAML中的所有变量.

如果这不是一场胜利,你总是可以尝试使用Hyde:http://hyde.github.com/这是用Python编写的,使用Jinja2(基本上是Django模板++),并做同样的事情.

  • YAML绝对是最好的方法,但是如果你的大部分内容只是大块不可分割的HTML,它就会变得不必要地复杂化.除了标题之外,我真的不需要_process_的大量原始数据.我只需要将大块HTML插入页面的特定非连续区域.描述YAML中的HTML是笨拙且不可维护的. (7认同)