繁体   English   中英

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

[英]How to use multiple partials with Swig?

在我看来,Swig 没有渲染一些局部。 如何以正确的方式传递block 什么文件应该扩展什么?

我有这样的观点结构:

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

index.html是 Swig 呈现的。 它看起来像这样:

{% 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看起来像这样:

<!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看起来像这样:

{% extends 'layout.html' %}

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

header_logo.html看起来像这样。 而这个不渲染。

{% extends 'layout.html' %}

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

每个文件只能扩展一个模板。 你想要做的是这样的......

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

或者,您似乎希望在index.html模板中包含header_logo ,而不是从中扩展。 你最好做这样的事情......

索引.html

{% extends 'layout.html' %}

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

使用视图文件夹下的文件夹(ExpressJS)

我在views文件夹下创建了一个partials文件夹。

然后从reports.swig 模板中引用这样的部分视图。

{% extends 'layout.swig' %}

  {% block content %}

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

{% endblock %}

感谢保罗的回答!

暂无
暂无

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

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