问题
I use Swig as a template engine.
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.
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.
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 ...
How to acheave it? As I understand for now this is a real drawback of Swig. Is there some workaround?
回答1:
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 wrote:
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.
来源:https://stackoverflow.com/questions/24034208/how-to-include-a-partial-into-another-partials-block