Defining a simple macro
We define a view MyMacros the usual way in app.py:
1 2 3 4 5 6 7 8 9 10 11 | import grok
class Sample(grok.Application, grok.Container):
pass
class Index(grok.View):
pass # see app_templates/index.pt
class MyMacros(grok.View):
"""The macro holder"""
grok.context(Sample) # The default model this view is bound to.
|
In the associated page template app_templates/mymacros.pt we define the macros we like to have available. You define macros with the METAL attribute:
metal:define-macro="<macro-name>"
and the slots therein with:
metal:define-slot=<slot-name>
Let's define a very plain page macro in app_templates/mymacros.pt:
<html metal:define-macro="mypage">
<head></head>
<body>
The content:
<div metal:define-slot="mycontent">
Put your content here...
</div>
</body>
</html>
Here we defined a single macro mypage with only one slot mycontent.
If we restart our Zope instance (don't forget to put some index.pt into app_templates/) and have created our application as test, we now can go to the following URL:
http://localhost:8080/test/mymacros
and see the output:
The content: Put your content here...
Allright.


A word of warning
The workaround, if you want to refer to attributes or something in the macro view is to reference by name. So, this is WRONG:
<html metal:define-macro="mypage">
<head tal:attributes="name view/some_attribute">
...unless you define 'some_attribute' for all views that references this. To reference it by name do it this way:
<html metal:define-macro="mypage">
<head tal:attributes="name context/@@mymacros/some_attribute">