2012-10-31 23 views
6

我想在單個Five ViewPageTemplateFile頁面模板中使用宏以避免複製粘貼我的代碼。金屬:使用宏和金屬:在五個ViewPageTemplateFile內定義宏

下面是我的嘗試:

<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:tal="http://xml.zope.org/namespaces/tal" 
     xmlns:metal="http://xml.zope.org/namespaces/metal" 
     xmlns:i18n="http://xml.zope.org/namespaces/i18n" 
     tal:omit-tag="" 
     > 

    <metal:row define-macro="row"> 
     <tal:block repeat="widget view/widgets"> 
      <div tal:attributes="class python:'%s%s' % (widget.klass, widget.mode=='hidden' and ' hidden' or '')" 
       tal:condition="python:view._includeRow(widget.name)"> 

       <div tal:replace="structure widget/render"></div> 

      </div> 
     </tal:block> 
    </metal:row> 

    <table class="datagridwidget-table-view" tal:attributes="data-extra view/extra"> 
     <thead> 
      <tr> 
       <th class="header"> 
        <!-- --> 
       </th> 
       <th id="" class="header" tal:condition="view/allow_insert"></th> 
       <th id="" class="header" tal:condition="view/allow_delete"></th> 
       <th id="" class="header" tal:condition="view/allow_reorder"></th> 
       <th id="" class="header" tal:condition="view/allow_reorder"></th> 
      </tr> 
     </thead> 
     <tbody class="datagridwidget-body" tal:attributes="data-name_prefix view/name_prefix; data-id_prefix view/id_prefix"> 
      <tal:row repeat="widget view/getNormalRows"> 
       <tr> 
        <metal:macro use-macro="here/row" /> 
       </tr> 
      </tal:row> 

      <tal:row condition="view/getTTRow" define="widget view/getTTRow"> 
       <tr> 
        <metal:macro use-macro="here/row" /> 
       </tr> 
      </tal:row> 


      <tal:row condition="view/getAARow" define="widget view/getAARow"> 
       <tr> 
        <metal:macro use-macro="here/row" /> 
       </tr> 
      </tal:row> 


    </tbody> 
</table> 
<input type="hidden" tal:replace="structure view/counterMarker" /> 
</html> 

然而,這裏沒有被定義(因爲它是據我知道舊的做事獨立頁面模板的方式)。

如何從ViewPageTemplate .pt模板中引用模板本身並在.pt文件中使用/定義宏?

+0

使用「語境」,而不是有沒有'在這裏',幫忙? –

回答

7

我想你想:

<metal:macro use-macro="template/macros/row" /> 
+0

我正在使用Plone 4.2.2和一個自定義的GrokView,這個片段給了我一個LocationError。模板摘錄:http://pastebin.com/VgDjKYTV |追溯:http://pastebin.com/7PeK94WE –

3

當您使用ViewPageTemplateFile您可以訪問(至少)在你的模板以下變量:

  • 這裏
  • 模板
  • 視圖

因此,而不是「這裏」(這相當於view/context),可以使用template/macros來訪問你的「行」宏作爲@davisagli表示

1

它也可能喜歡

<metal:macro use-macro="here/ViewPageTemplate/macros/row" />