Jekyll模板使用django样的液体块/继承 [英] Jekyll templates using django-like liquid blocks / inheritance

查看:132
本文介绍了Jekyll模板使用django样的液体块/继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进入Jekyll,并希望将其作为一个通用的前端开发平台,但是与Liquid模板语言的局限性有所抵触,特别是与Django模板的区别。 p>

我发现了液体继承宝石,它从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>

孩子

  {%extends _layouts / parent.html%} 
{%block foo%}
< div>
Bar接下来:
{%block bar%} {%endblock%}
< / div>
{%endblock%}

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

  --- 
标题:test
---

{%extends _layouts / child.html%}
{%block bar%}我的标题是{{page.title}} {%endblock%}

我期望的是:

 < html> 
< div>
Bar接下来:
我的标题是test
< / div>
< / html>

我所得到的:

 < HTML> 
< div>
酒吧来吧:
< / div>
< / html>我的标题是test

似乎有些事情没有对待mypage.html中的块可以插入到父母/孩子的合适位置,尽管它仍然在做某些事情。



我不是一个ruby开发人员,而是相当新的Jekyll,所以我需要帮助确定这个堆栈的哪个部分失败。 github上的流体继承问题表明其他人正在遇到此嵌套嵌套问题: https://github.com/danwrong/液体继承/问题/ 3



我尝试了几种液体继承叉,其中许多显然是修复了这个问题正则表达式,但是没有似乎解决了这个问题。



我正在努力从根本上不可能做到吗?似乎我至少有85%的方式,最后一点需要修复。

解决方案

我不是这肯定是永远在Jekyll工作。我可能是错的,但这是我的推理:



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



这是递归的 - 它处理页面的内容,然后处理页面的布局,然后处理布局的布局等等,将YAML变量传递给链(因此他们'在父模板中始终可以使用{{page.whatever}})。



这意味着,传递的唯一的东西是YAML值,无论值内容是由液体处理之后的。我不知道在别的地方怎么做,但这似乎与块的想法不符,因为他们需要你单独传递两个块。



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



所有这些说,我不知道你已经用尽了使用YAML,_includes的限制,和模板逻辑。如果您将Django样式块放在您的内容中,为什么不要这样做:



内容: p>

  --- 
title:some title
secondary_content:|
这是一些*二级*内容将被[markdownified](http://example.com)。
它可以运行到多行并且包括
*列表
*好东西
* etc
---

这里是主要内容,按照通常的

模板:

 < 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如果page.type ='bar'%}
{%include sidebar_bar.html%}
{%endif%}

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



如果这不是一个胜利,你可以随时尝试海德: http://hyde.github.com/ ,使用Jinja2(基本上是Django模板++),并做同样的事情。 / p>

I'm getting into Jekyll in a big way and would like to use it as a general front-end development platform, but am running up against the limitations of the Liquid templating language, specifically its difference to Django templating.

I discovered the liquid-inheritance gem, which adds the all-important Extends and Block syntax from Django. This blog post extends the gem further to suit Jekyll's file system: http://www.sameratiani.com/2011/10/22/get-jekyll-working-with-liquid-inheritance.html

The problem is that it doesn't appear to implement blocks in exactly the same way Django does, which essentially renders the gem useless.

I have two jekyll "layouts" called - for the sake of understanding - parent.html and child.html. Neither of these contain YAML sections.

Parent

<html>
{% block foo %} {% endblock %}
</html>

Child

{% extends _layouts/parent.html %}
{% block foo %}
  <div>
    Bar comes next:
    {% block bar %} {% endblock %}
  </div>
{% endblock %}

And then I have a jekyll page which includes a YAML section thus:

---
title: test
---

{% extends _layouts/child.html %}
{% block bar %}My title is {{ page.title }} {% endblock %}

What I'd expect:

<html>
  <div>
    Bar comes next:
    My title is test
  </div>
</html>

What I get:

<html>
  <div>
    Bar comes next:
  </div>
</html>My title is test

It seems something is failing to treat the blocks in mypage.html as being eligible for insertion into the suitable places of parent/child, although it's clearly still doing something.

I'm not a ruby developer and am reasonably new to Jekyll, so I need help identifying what part of this stack is failing. The liquid-inheritance issues on github suggest others are experiencing this block nesting problem: https://github.com/danwrong/liquid-inheritance/issues/3

I've tried several of the forks of liquid-inheritance, many of which apparently fix that problem regex, but none seem to solve this.

Is what i'm tring to do fundamentally impossible? It seems like I'm at least 85% of the way there and the final bit needs fixing.

解决方案

I'm not sure this is ever going to work within Jekyll. I might be wrong, but here's my reasoning:

Each page is rendered out using do_layout in https://github.com/mojombo/jekyll/blob/master/lib/jekyll/convertible.rb

This works recursively - it processes the content of the page, then processes the page's layout, then that layout's layout and so on and so forth, passing the YAML variables up the chain (so they're always available in parent templates as {{ page.whatever}}).

This means that the only things which get passed up are the YAML values, and whatever the value of 'content' is after it has been processed by Liquid. I don't know how it is done elsewhere, but that seems incompatible with the idea of blocks, as they'd require you to pass up the two blocks separately.

Fundamentally, it seems to me that the issue is that Jekyll already has a simple form of inheritance - via the "layout" attribute that you can give to a layout. Fundamentally, I think that this is compatible with liquid-templating.

All that said, I'm not sure that you've exhausted the limits of using YAML, _includes, and template logic. If you're at the point of putting Django style blocks into your content, why not just do something like this:

Content:

---
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

Template:

<html>
<article>
    <h1>{{ page.title }}</h1>
    {{ content }}
</article>
<aside>
{{ page.secondary_content | markdownify}}
</aside>

If you wanted to keep your templates clean, and have different content for different types of pages, you could use various includes:

Template:

<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 %}

And then put your page type specific stuff in those files. Obviously you could include it directly, but it is probably nice to abstract it out. Those includes will get all of the variables in the YAML.

If none of this is a win, you could always try Hyde: http://hyde.github.com/ which is written in Python, uses Jinja2 (basically Django templates++), and does the same sort of thing.

这篇关于Jekyll模板使用django样的液体块/继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆