2011-07-08 36 views

回答

3

只需註冊他們兩個:

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> 
+0

完美的作品!謝謝! – Sukumar