【问题标题】:Python Pyramid - Add multiple chameleon base templatesPython Pyramid - 添加多个变色龙基础模板
【发布时间】:2011-10-01 04:37:30
【问题描述】:

我正在使用this 过程来使用其他模板可以从中派生的基本模板。

如何创建多个基本模板?

【问题讨论】:

    标签: python pyramid chameleon template-tal template-metal


    【解决方案1】:

    只需注册他们两个:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 2016-11-13
      • 2012-02-15
      • 2012-06-24
      • 2016-05-06
      相关资源
      最近更新 更多