我正在深入了解 Jekyll,并希望将其用作通用前端开发平台,但遇到了 Liquid 模板语言的局限性,特别是它与 Django 模板的区别。
我发现了 liquid-inheritance gem,它添加了 Django 中最重要的 Extends 和 Block 语法。这篇博文进一步扩展了 gem 以适应 Jekyll 的文件系统: http://www.sameratiani.com/2011/10/22/get-jekyll-working-with-liquid-inheritance.html
问题是它似乎没有以与 Django 完全相同的方式实现 block ,这实际上使 gem 变得无用。
为了便于理解,我有两个名为 parent.html 和 child.html 的 jekyll“布局”。这些都不包含 YAML 部分。
parent
<html>
{% block foo %} {% endblock %}
</html>
child
{% extends _layouts/parent.html %}
{% block foo %}
<div>
Bar comes next:
{% block bar %} {% endblock %}
</div>
{% endblock %}
然后我有一个包含 YAML 部分的 jekyll 页面:
---
title: test
---
{% extends _layouts/child.html %}
{% block bar %}My title is {{ page.title }} {% endblock %}
我的期望:
<html>
<div>
Bar comes next:
My title is test
</div>
</html>
我得到的:
<html>
<div>
Bar comes next:
</div>
</html>My title is test
似乎有些东西未能将 mypage.html 中的 block 视为有资格插入到父/子的合适位置,尽管它显然仍在做一些事情。
我不是 ruby 开发人员并且对 Jekyll 还比较陌生,因此我需要帮助来确定该堆栈的哪个部分出现故障。 github 上的液体继承问题表明其他人遇到了这个 block 嵌套问题:https://github.com/danwrong/liquid-inheritance/issues/3
我已经尝试了几种液体继承的分支,其中许多显然解决了正则表达式问题,但似乎没有一个能解决这个问题。
我想做的事情根本不可能吗?看来我至少完成了 85%,最后一点需要修复。
最佳答案
我不确定这是否会在 Jekyll 中起作用。我可能是错的,但这是我的推理:
每个页面都使用 https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb 中的 do_layout 呈现出来
这是递归工作的——它处理页面的内容,然后处理页面的布局,然后处理该布局的布局等等,将 YAML 变量向上传递到链中(因此它们始终在父模板中可用,如{{ page.whatever}})。
这意味着唯一被传递的是 YAML 值,以及“内容”在被 Liquid 处理后的任何值。我不知道其他地方是怎么做到的,但这似乎与 block 的想法不相容,因为它们要求您分别传递两个 block 。
从根本上说,在我看来,问题在于 Jekyll 已经有了一种简单的继承形式——通过您可以赋予布局的“layout”属性。从根本上说,我认为这与液体模板兼容。
综上所述,我不确定您是否已经用尽了使用 YAML、_includes 和模板逻辑的限制。如果您要将 Django 样式 block 放入您的内容,为什么不做这样的事情:
内容:
---
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
模板:
<html>
<article>
<h1>{{ page.title }}</h1>
{{ content }}
</article>
<aside>
{{ page.secondary_content | markdownify}}
</aside>
如果你想保持你的模板干净,并为不同类型的页面提供不同的内容,你可以使用各种包含:
模板:
<aside>
{% include sidebar_negotiation.html %}
</aside>
_includes/sidebar_negotiation.html:
{% if page.type = 'foo' %}
{% include sidebar_foo.html %}
{% else if page.type = 'bar' %}
{% include sidebar_bar.html %}
{% endif %}
然后将您的页面类型特定内容放入这些文件中。显然您可以直接包含它,但最好将其抽象出来。这些包含将获取 YAML 中的所有变量。
如果这些都不成功,您可以随时尝试海德:http://hyde.github.com/它是用 Python 编写的,使用 Jinja2(基本上是 Django 模板++),并且做同样的事情。
关于ruby - 使用类似 django 的液体 block /继承的 Jekyll 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13086569/