如何将一个部分包含在另一个部分的块中? [英] How to include a partial into another partial's block?

查看:109
本文介绍了如何将一个部分包含在另一个部分的块中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Swig 作为模板引擎.

我需要包含一个部分,但仅包含在另一个部分的块中.但是Swig只是在下面包含了它,而忽略了一个标签.这是它的外观:

I need to include a partial but only into another partial's block. But Swig includes it just below, ignoring a block tag. Here is how it looks:

1.

layout.html

<!DOCTYPE html>
<html>
    <head>
        {% block title %}{% endblock %}
        {% block headtag %}
            <link rel='stylesheet' href='/assets/css/global.css' />
            <link rel='stylesheet' href='/bower_components/bootstrap/dist/css/bootstrap.css' />
            <script src="/bower_components/jquery/dist/jquery.js"></script>
            <script src="/bower_components/bootstrap/dist/js/bootstrap.js"></script>
        {% endblock %}
    </head>

    <body>
        <div class="container cmt">

            {% block content %}{% endblock %}

        </div>
    </body>
</html>

2.

这是请求后呈现的页面视图.

This is a page view that is rendered after request.

index.html

{% extends 'layouts/layout.html' %}


{% block title %}
    <title>{{ title }}</title>
{% endblock %}

{% block headtag %}
    {% parent %}
    <link href="/assets/css/index/index.css" rel="stylesheet">
    <link href="/assets/css/header.css" rel="stylesheet">
{% endblock %}



{% block content %}

    {% include 'index_content.html' %}

{% endblock %}

3.

在这里,我包括一个标题部分(partials/header.html).而且此标头的部分有一个空的块标记(header_nav),我想在其中包含另一个部分(header_nav.html).但是只能成块了.请参见下方的 header.html .

Here I include a header partial (partials/header.html). And this header partial has an empty block tag (header_nav) in which I want to include another partial (header_nav.html). But only into the block. See the header.html lower.

index_content.html

{% include 'partials/header.html' %}

{% block header_nav %}
    {% include 'header_nav.html' %}
{% endblock %}    

4.

这是partials/header.html的html代码.请参见块标签header_nav.在该块中,我要放置 header_nav.html .但这是行不通的.它将 header_nav.html 放在 partials/header.html :

Here is an html-code of the partials/header.html. See the block tag header_nav. In that block I want to place header_nav.html. But it doesn't work. It places header_nav.html just bellow partials/header.html:

<div class="clearfix mb30">

    <div class="row">

        <div class="col-md-3">
            <div id="logo_div" class="">
                <div class="logo_div">
                    <a href="/" title="">
                        <span class="logo_span"></span>
                    </a>
                </div>
            </div>
        </div>

        <div class="col-md-9 hctrlbar">

            {% block header_nav %}
                ... I want to include a partial here only ...
            {% endblock %}

        </div>

    </div>
</div>

... But Swig ignores header_nav block and places here for some reason ...

如何做到?据我所知,这是Swig的真正缺点.有什么解决方法吗?

How to acheave it? As I understand for now this is a real drawback of Swig. Is there some workaround?

推荐答案

如果我说对了,那就做不到.

If i got you right, this cannot be done.

看看这个类似的问题. 在注释中,有一个指向

Take a look at this similar question. In this comment there's a link to a related issue on github.

paularmstrong 写道:

包含的模板中不支持块.您应该(能够)构造您的扩展/继承关系以完成同一件事...

Blocks are not supported in included templates. You should (be able to) structure your extends/inheritance to accomplish the same thing...

...您应该使用多个扩展...
永远不会支持包含中的块.

...You should use multiple extends...
Blocks in includes will never be supported.

还有一个可能与您类似的示例.

There's also an example similar to yours that you might want to check out.

这篇关于如何将一个部分包含在另一个部分的块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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