簡體   English   中英

Python Pyramid - 添加多個變色龍基礎模板

[英]Python Pyramid - Add multiple chameleon base templates

我正在使用過程來使用其他模板可以從中派生的基本模板。

如何創建多個基本模板?

只需注冊它們:

from pyramid.renderers import get_renderer

def add_base_template(event):
    base = get_renderer('templates/base.pt').implementation()
    base2 = get_renderer('templates/base2.pt').implementation()
    event.update({'base': base, 'base2': base2})

然后為每個頁面選擇要在模板中使用的內容:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:use-macro="base">
    <tal:block metal:fill-slot="content">
        My awesome content.
    </tal:block>
</html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:use-macro="base2">
    <tal:block metal:fill-slot="content">
        Content on a totally different page.
    </tal:block>

我相信模板不必是整個 HTML 元素,因此您可以將 2 個宏擴展為相同的最終模板

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal">
    <body>
        <div metal:use-macro="section1">
            <tal:block metal:fill-slot="content">
                Content for template "section1".
            </tal:block>
        </div>
        <div metal:use-macro="section2">
            <tal:block metal:fill-slot="content">
                Content for template "section2".
            </tal:block>
        </div>
    </body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM