从多个子模板继承模板

template inheritance from multiple sub-templates

本文关键字:继承      更新时间:2023-09-26

所以我有一个网站,它的结构由各种模板组成:

- index //the html skeleton, with script tags and css links
- header //the header of the page
- topnavigation //contains the main nav with the menu
- content //this is where the dynamic content will be changed for different pages
- footer //the footer

现在,我知道集成它的唯一方法是:

res.render(content);    
content -> inherits footer
footer -> inherits topnavigation
topnavigation-> inherits header
header -> inherits index

我认为如果我能有这样的东西,它会更实用和易于维护:

res.render(content);
content -> inherits dochead
index -> includes header + topnnavigation + footer
  • 我错了吗?
  • 如果我是对的,我该怎么做?

谢谢

典型的extends设置更像是这样的:(对于一个工作示例,您可以在此处查看Swig文档如何设置其布局:https://github.com/paularmstrong/swig/tree/master/docs/layouts)

  • skeleton.html带有块的 html 框架:
    • {% block html %}
    • {% block css %}
    • {% block js %}
    • {% block body %}
  • base.html扩展了skeleton.html,带有块/子块的基本布局:
    • {% block body %}
      • {% block header %} 包括一些默认标头内容。
        • {% block nav %} 包括主要导航内容。
      • {% block content %}
      • {% block footer %} 包括版权等。
  • index.html扩展base.html
    • {% block nav %}(可能用于导航的内容,否则只需base.html呈现其导航内容
    • 您还将根据需要覆盖此处base.html中设置的任何其他块。如果您发现自己需要对模板进行更多控制,请考虑创建另一个扩展base.html的子布局,然后index.html扩展该布局。