Jekyll templates using django-like liquid blocks / inheritance

后端 未结 1 349
无人及你
无人及你 2021-02-08 00:54

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 la

相关标签:
1条回答
  • 2021-02-08 01:31

    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.

    0 讨论(0)
提交回复
热议问题