Simplify macro registration
Provide ZCML directive for macro registration instead a python class
and smplify the lookup via a macros: TALES expression and
a IMacros adapter.
Status
Author
Roger Ineichen (dev@projekt01.ch)
Problem/Proposal
Right now we define the macros in ZPT like this:
<metal:block define-macro="page">
and use the macro like this:
<html metal:use-macro="context/@@standard_macros/view">
This is really weird because there is no relation in the naming.
The relation is done in a python class e.g. StandardMacros? which is inherited from the class Macros:
class Macros(object):
zope.interface.implements(zope.interface.common.mapping.IItemMapping)
macro_pages = ()
aliases = {
'view': 'page',
'dialog': 'page',
'addingdialog': 'page'
}
def __getitem__(self, key):
key = self.aliases.get(key, key)
context = self.context
request = self.request
for name in self.macro_pages:
page = zapi.getMultiAdapter((context, request), name=name)
try:
v = page[key]
except KeyError:
pass
else:
return v
raise KeyError(key)
class StandardMacros(BrowserView, Macros):
macro_pages = ('my_macros, 'other_macros')
This class is registered as a page:
<browser:page
for="*"
name="standard_macros"
permission="zope.View"
class=".standardmacros.StandardMacros"
layer="tiks.skins.basic.basic"
allowed_interface="zope.interface.common.mapping.IItemMapping"
/>
Where allows to lookup macros in ZPT like:
<html metal:use-macro="context/@@standard_macros/my_macros">
Goals
I like to simplify this with a own directive for registering macros like:
<browser:macros
for="*"
name="standard_macros"
macros="page"
aliases="view:page dialog:page" (only if we need to support the mapping)
permission="zope.View"
template="template.pt"
layer="tiks.skins.basic.basic"
/>
Then register a adapter where we can lookup like this:
zapi.getMultiAdapter((context, request), IMacros, name='standard_macros')
This adapter lookup is a part of the TALES expression where we register. The new style macro lookup will use the TALES expression and call macros like:
<html metal:use-macro="macros:standard_macros/my_macros">
Or if we can get rid of the mapping we can use the macro call like:
Why do we need this?
I like views but it seems to me, that the view namespace get more and more used as a new aquisition concept for z3. Not everything is a view because we can adapt it to context and request. Macros are a good sample of this. Right now, macros provide to much and get lookuped via a second view hook. This will slow down and is not needed.
Risks
Yet another ZCML directive
