Python模板,有从多个html文件继承的方法吗?

2024-04-26 11:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

所以说我有这个结构

<html>
   <body>
      <div>Here goes content 1</div>
      <div>Here goes content 2</div>
      <div>Here goes content 3</div>
   <body>
</html>

我可以在另一个文件上进行扩展,而不是让内容1,2,3在这里,我可以有块。但这意味着无论哪个文件继承了这个基本模板,都必须包含所有三个块。有没有可能这样做

<html>
   <body>
      <div>{% grabfrom 'firstdiv.html' %}</div>
      <div>{% grabfrom 'seconddiv.html' %}</div>
      <div>{% grabfrom 'lastdiv.html' %}</div>
   <body>
</html>

然后我可以提供这个模板?你知道吗


Tags: 文件div模板内容herehtmlbodycontent
1条回答
网友
1楼 · 发布于 2024-04-26 11:24:01

可以使用^{} syntax将其他模板加载到特定位置:

<html>
   <body>
      <div>{% include 'firstdiv.html' %}</div>
      <div>{% include 'seconddiv.html' %}</div>
      <div>{% include 'lastdiv.html' %}</div>
   <body>
</html>

如果这些include不需要访问当前上下文(变量),可以通过向语句中添加关键字without context来启用这些include的缓存:

 <html>
   <body>
      <div>{% include 'firstdiv.html' without context %}</div>
      <div>{% include 'seconddiv.html' without context %}</div>
      <div>{% include 'lastdiv.html' without context %}</div>
   <body>
</html>

请参阅模板设计器文档的Import Context Behaviour section。你知道吗

相关问题 更多 >