metal:use-macro and metal:define-macro within Five ViewPageTemplateFile
Asked Answered
E

3

6

I'd like to use a macro within a single Five ViewPageTemplateFile page template to avoid copy-pasting my code around.

Below is my attempt:

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

However, here is not defined (as it is old independent page template way of doing things as far as I know).

How can I refer the template itself from ViewPageTemplate .pt template and use/define macros within .pt file?

Expellant answered 31/10, 2012 at 18:57 Comment(1)
Does using 'context' instead of 'here', help?Counts
S
7

I think you want:

<metal:macro use-macro="template/macros/row" />
Scarcity answered 31/10, 2012 at 19:10 Comment(1)
I'm using Plone 4.2.2 with a custom GrokView and this snippet gives me a LocationError. Template excerpt: pastebin.com/VgDjKYTV | Traceback: pastebin.com/7PeK94WEOctosyllable
B
3

When you use ViewPageTemplateFile you have access to (at least) the following variables in your template:

  • here
  • template
  • view

So instead of "here" (which is equivalent to view/context) you can use template/macros to access your "row" macro as @davisagli indicates

Bringhurst answered 31/10, 2012 at 19:19 Comment(0)
K
1

It may also like

<metal:macro use-macro="here/ViewPageTemplate/macros/row" />
Kurman answered 6/4, 2017 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.