简体   繁体   English

如何在 Swig 中使用多个部分?

[英]How to use multiple partials with Swig?

Swig doesn't render some partials in my view.在我看来,Swig 没有渲染一些局部。 How to pass block s the right way?如何以正确的方式传递block What file should extend what?什么文件应该扩展什么?

I have such a structure of my view:我有这样的观点结构:

// files
header.html   // <- partial
header_logo.html   // <- partial
layout.html   // <- layout
index.html   // <- page

index.html is the one that Swig renders. index.html是 Swig 呈现的。 It looks like this:它看起来像这样:

{% extends 'layout.html' %}
{% extends 'header.html' %}
{% extends 'header_logo.html' %}

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

{% block content %}

     {% block header_logo %}{% endblock %} // <- This one doesn't render

     .... html content code goes here ....
{% endblock %}

layout.html looks like this: layout.html看起来像这样:

<!DOCTYPE html>
<html lang="en">
  <head>
    {% block head %}
        <link href="/assets/css/index/layout.css" rel="stylesheet">
    {% endblock %}
  </head>
  <body>

    {% block header %}{% endblock %}

    {% block content %}{% endblock %}  

  </body>
</html>

header.html looks like this: header.html看起来像这样:

{% extends 'layout.html' %}

{% block header %}
    ... html code goes here ...
{% endblock %}

header_logo.html looks like this. header_logo.html看起来像这样。 And this one doesn't render.而这个不渲染。

{% extends 'layout.html' %}

{% block header_logo %}
    ... html code goes here ...
{% endblock %}

You can only extend one template per file.每个文件只能扩展一个模板。 What you want to do is something like this...你想要做的是这样的......

  • index.html extends header_logo.html index.html扩展了header_logo.html
  • header_logo.html extends header.html header_logo.html扩展了header.html
  • header.html extends layout.html header.html扩展了layout.html

Or, it looks like you want to include header_logo in your index.html template, not extend from it.或者,您似乎希望在index.html模板中包含header_logo ,而不是从中扩展。 You might be better off doing something like this...你最好做这样的事情......

index.html索引.html

{% extends 'layout.html' %}

{% block header %}{% include 'header.html' %}{% end block %}
{% block content %}
    {% include 'header_logo.html' %}
...
{% endblock %}

Using a folder under the views folder (ExpressJS)使用视图文件夹下的文件夹(ExpressJS)

I created a partials folder under the views folder.我在views文件夹下创建了一个partials文件夹。

Then referenced the partial view like this from the reports.swig template.然后从reports.swig 模板中引用这样的部分视图。

{% extends 'layout.swig' %}

  {% block content %}

    {% for report in reports %}
      {% include './partials/displayReports.swig' %}
    {% endfor %}

{% endblock %}

Thanks Paul for the answer!感谢保罗的回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM