[Checkins] SVN: z3c.macro/ Added z3c.macro implementation
Roger Ineichen
roger at projekt01.ch
Wed Jan 17 20:02:28 EST 2007
Log message for revision 72084:
Added z3c.macro implementation
Changed:
A z3c.macro/branches/
A z3c.macro/tags/
A z3c.macro/trunk/
A z3c.macro/trunk/src/
A z3c.macro/trunk/src/z3c/
A z3c.macro/trunk/src/z3c/__init__.py
A z3c.macro/trunk/src/z3c/macro/
A z3c.macro/trunk/src/z3c/macro/README.txt
A z3c.macro/trunk/src/z3c/macro/SETUP.cfg
A z3c.macro/trunk/src/z3c/macro/__init__.py
A z3c.macro/trunk/src/z3c/macro/configure.zcml
A z3c.macro/trunk/src/z3c/macro/interfaces.py
A z3c.macro/trunk/src/z3c/macro/meta.zcml
A z3c.macro/trunk/src/z3c/macro/tales.py
A z3c.macro/trunk/src/z3c/macro/tests.py
A z3c.macro/trunk/src/z3c/macro/z3c.macro-configure.zcml
A z3c.macro/trunk/src/z3c/macro/z3c.macro-meta.zcml
A z3c.macro/trunk/src/z3c/macro/zcml.py
A z3c.macro/trunk/src/z3c/macro/zcml.txt
-=-
Added: z3c.macro/trunk/src/z3c/__init__.py
===================================================================
--- z3c.macro/trunk/src/z3c/__init__.py 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/__init__.py 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,16 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
Property changes on: z3c.macro/trunk/src/z3c/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/README.txt
===================================================================
--- z3c.macro/trunk/src/z3c/macro/README.txt 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/README.txt 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,199 @@
+=====
+Macro
+=====
+
+This package provides a adapter and a TALES expression for a expliciter and
+flexibler macro handling using the adapter registry for macros.
+
+We start with creating a content object that is used as a view context later:
+
+ >>> import zope.interface
+ >>> import zope.component
+ >>> from zope.publisher.interfaces.browser import IBrowserView
+ >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
+ >>> class Content(object):
+ ... zope.interface.implements(zope.interface.Interface)
+
+ >>> content = Content()
+
+We also create a temp dir for sample templates which we define later for
+testing:
+
+ >>> import os, tempfile
+ >>> temp_dir = tempfile.mkdtemp()
+
+
+Macro Template
+--------------
+
+We define a macro template as a adapter providing IMacroTemplate:
+
+ >>> path = os.path.join(temp_dir, 'navigation.pt')
+ >>> open(path, 'w').write('''
+ ... <metal:block define-macro="navigation">
+ ... <div tal:content="title">---</div>
+ ... </metal:block>
+ ... ''')
+
+Let's define the macro factory
+
+ >>> from zope.app.pagetemplate import viewpagetemplatefile
+ >>> from z3c.macro import interfaces
+ >>> from z3c.macro import zcml
+ >>> navigationMacro = zcml.MacroFactory(path, 'navigation', 'text/html')
+
+and register them as adapter:
+
+ >>> zope.component.provideAdapter(
+ ... navigationMacro,
+ ... (zope.interface.Interface, IBrowserView, IDefaultBrowserLayer),
+ ... interfaces.IMacroTemplate,
+ ... name='navigation')
+
+
+The TALES ``macro`` Expression
+------------------------------
+
+The ``macro`` expression will look up the name of the macro, call a adapter
+providing IMacroTemplate and uses them or fills a slot if defined in the
+``macro`` expression.
+
+Let's create a page template using the ``navigation`` macros:
+
+ >>> path = os.path.join(temp_dir, 'first.pt')
+ >>> open(path, 'w').write('''
+ ... <html>
+ ... <body>
+ ... <h1>First Page</h1>
+ ... <div class="navi">
+ ... <tal:block define="title string:My Navigation">
+ ... <metal:block use-macro="macro:navigation" />
+ ... </tal:block>
+ ... </div>
+ ... <div class="content">
+ ... Content here
+ ... </div>
+ ... </body>
+ ... </html>
+ ... ''')
+
+As you can see, we used the ``macro`` expression to simply look up a macro
+called navigation whihc get inserted and replaces the HTML content at this
+place.
+
+Let's now create a view using this page template:
+
+ >>> from zope.app.pagetemplate.simpleviewclass import SimpleViewClass
+ >>> FirstPage = SimpleViewClass(path, name='first.html')
+
+ >>> zope.component.provideAdapter(
+ ... FirstPage,
+ ... (zope.interface.Interface, IDefaultBrowserLayer),
+ ... zope.interface.Interface,
+ ... name='first.html')
+
+Finally we look up the view and render it:
+
+ >>> from zope.publisher.browser import TestRequest
+ >>> request = TestRequest()
+
+ >>> view = zope.component.getMultiAdapter((content, request),
+ ... name='first.html')
+ >>> print view().strip()
+ <html>
+ <body>
+ <h1>First Page</h1>
+ <div class="navi">
+ <div>My Navigation</div>
+ </div>
+ <div class="content">
+ Content here
+ </div>
+ </body>
+ </html>
+
+
+Slot
+----
+
+We can also define a macro slot and fill it with given content:
+
+ >>> path = os.path.join(temp_dir, 'addons.pt')
+ >>> open(path, 'w').write('''
+ ... <metal:block define-macro="addons">
+ ... Content before header
+ ... <metal:block define-slot="header">
+ ... <div>My Header</div>
+ ... </metal:block>
+ ... Content after header
+ ... </metal:block>
+ ... ''')
+
+Let's define the macro factory
+
+ >>> addonsMacro = zcml.MacroFactory(path, 'addons', 'text/html')
+
+and register them as adapter:
+
+ >>> zope.component.provideAdapter(
+ ... addonsMacro,
+ ... (zope.interface.Interface, IBrowserView, IDefaultBrowserLayer),
+ ... interfaces.IMacroTemplate,
+ ... name='addons')
+
+Let's create a page template using the ``addons`` macros:
+
+ >>> path = os.path.join(temp_dir, 'second.pt')
+ >>> open(path, 'w').write('''
+ ... <html>
+ ... <body>
+ ... <h1>Second Page</h1>
+ ... <div class="header">
+ ... <metal:block use-macro="macro:addons">
+ ... This line get ignored
+ ... <metal:block fill-slot="header">
+ ... Header comes from here
+ ... </metal:block>
+ ... This line get ignored
+ ... </metal:block>
+ ... </div>
+ ... </body>
+ ... </html>
+ ... ''')
+
+Let's now create a view using this page template:
+
+ >>> SecondPage = SimpleViewClass(path, name='second.html')
+
+ >>> zope.component.provideAdapter(
+ ... SecondPage,
+ ... (zope.interface.Interface, IDefaultBrowserLayer),
+ ... zope.interface.Interface,
+ ... name='second.html')
+
+Finally we look up the view and render it:
+
+ >>> view = zope.component.getMultiAdapter((content, request),
+ ... name='second.html')
+ >>> print view().strip()
+ <html>
+ <body>
+ <h1>Second Page</h1>
+ <div class="header">
+ <BLANKLINE>
+ Content before header
+ <BLANKLINE>
+ Header comes from here
+ <BLANKLINE>
+ Content after header
+ </div>
+ </body>
+ </html>
+
+
+Cleanup
+-------
+
+ >>> import shutil
+ >>> shutil.rmtree(temp_dir)
+
Property changes on: z3c.macro/trunk/src/z3c/macro/README.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/SETUP.cfg
===================================================================
--- z3c.macro/trunk/src/z3c/macro/SETUP.cfg 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/SETUP.cfg 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+ z3c.macro-*.zcml
+</data-files>
Property changes on: z3c.macro/trunk/src/z3c/macro/SETUP.cfg
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/__init__.py
===================================================================
--- z3c.macro/trunk/src/z3c/macro/__init__.py 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/__init__.py 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,16 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
Property changes on: z3c.macro/trunk/src/z3c/macro/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/configure.zcml
===================================================================
--- z3c.macro/trunk/src/z3c/macro/configure.zcml 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/configure.zcml 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,13 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ xmlns:tales="http://namespaces.zope.org/tales"
+ i18n_domain="z3c">
+
+ <interface interface=".interfaces.IMacroExpression" />
+
+ <tales:expressiontype
+ name="macro"
+ handler=".tales.MacroExpression"
+ />
+
+</configure>
\ No newline at end of file
Property changes on: z3c.macro/trunk/src/z3c/macro/configure.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/interfaces.py
===================================================================
--- z3c.macro/trunk/src/z3c/macro/interfaces.py 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/interfaces.py 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,47 @@
+##############################################################################
+#
+# Copyright (c) 2005 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+from zope.tales import interfaces
+from zope.pagetemplate.interfaces import IPageTemplate
+
+
+class IMacroTemplate(IPageTemplate):
+ """A macro template."""
+
+
+class IMacroExpression(interfaces.ITALESExpression):
+ """Return the HTML content of the named provider.
+
+ To call a macro from a page template use the the following syntax::
+
+ <metal:block use-macro="macro:macroname">
+ <metal:block fill-slot="slotname">
+ this content get rendered in the defined slot of the macro
+ <\metal:block>
+ <\metal:block>
+
+ or
+
+ <metal:block use-macro="macro:macroname">
+ this content get replaced by the defined macro
+ </metal:block>
+
+ The ``macro:`` TALES expression calles a named adapter adapting
+ (context, request) or (context, request, view), depending on the usage
+ of the view attribute in the macro directive. A macro provides the
+ interface IMacroTemplate.
+ """
Property changes on: z3c.macro/trunk/src/z3c/macro/interfaces.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/meta.zcml
===================================================================
--- z3c.macro/trunk/src/z3c/macro/meta.zcml 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/meta.zcml 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,16 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ xmlns:meta="http://namespaces.zope.org/meta">
+
+ <meta:directives namespace="http://namespaces.zope.org/z3c">
+
+ <meta:directive
+ name="macro"
+ schema=".zcml.IMacroDirective"
+ handler=".zcml.macroDirective"
+ />
+
+ </meta:directives>
+
+</configure>
+
Property changes on: z3c.macro/trunk/src/z3c/macro/meta.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/tales.py
===================================================================
--- z3c.macro/trunk/src/z3c/macro/tales.py 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/tales.py 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,39 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Provider tales expression registrations
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+import zope.interface
+from zope.tales import expressions
+from zope.app.zapi import getMultiAdapter
+
+from z3c.macro import interfaces
+
+
+class MacroExpression(expressions.StringExpr):
+ """Collect named IMacroTemplate via a TAL namespace called ``macro``."""
+
+ zope.interface.implements(interfaces.IMacroExpression)
+
+ def __call__(self, econtext):
+ name = super(MacroExpression, self).__call__(econtext)
+ context = econtext.vars['context']
+ request = econtext.vars['request']
+ view = econtext.vars['view']
+
+ return getMultiAdapter((context, view, request),
+ interface=interfaces.IMacroTemplate, name=name)
Property changes on: z3c.macro/trunk/src/z3c/macro/tales.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/tests.py
===================================================================
--- z3c.macro/trunk/src/z3c/macro/tests.py 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/tests.py 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,56 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Viewlet tests
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+import unittest
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+from zope.app.testing import setup
+
+
+def setUp(test):
+ root = setup.placefulSetUp(site=True)
+ test.globs['root'] = root
+
+ from zope.app.pagetemplate import metaconfigure
+ from z3c.macro import tales
+ metaconfigure.registerType('macro', tales.MacroExpression)
+
+ # register provider TALES
+ from zope.app.pagetemplate import metaconfigure
+ from zope.contentprovider import tales
+ metaconfigure.registerType('provider', tales.TALESProviderExpression)
+
+
+def tearDown(test):
+ setup.placefulTearDown()
+
+
+def test_suite():
+ return unittest.TestSuite((
+ DocFileSuite('README.txt',
+ setUp=setUp, tearDown=tearDown,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ ),
+ DocFileSuite('zcml.txt', setUp=setUp, tearDown=tearDown,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,),
+ ))
+
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Property changes on: z3c.macro/trunk/src/z3c/macro/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/z3c.macro-configure.zcml
===================================================================
--- z3c.macro/trunk/src/z3c/macro/z3c.macro-configure.zcml 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/z3c.macro-configure.zcml 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,6 @@
+<configure
+ xmlns:zcml="http://namespaces.zope.org/zcml">
+
+ <include package="z3c.macro" />
+
+</configure>
Property changes on: z3c.macro/trunk/src/z3c/macro/z3c.macro-configure.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/z3c.macro-meta.zcml
===================================================================
--- z3c.macro/trunk/src/z3c/macro/z3c.macro-meta.zcml 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/z3c.macro-meta.zcml 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,6 @@
+<configure
+ xmlns:zcml="http://namespaces.zope.org/zcml">
+
+ <include package="z3c.macro" file="meta.zcml" />
+
+</configure>
Property changes on: z3c.macro/trunk/src/z3c/macro/z3c.macro-meta.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/zcml.py
===================================================================
--- z3c.macro/trunk/src/z3c/macro/zcml.py 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/zcml.py 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,138 @@
+##############################################################################
+#
+# Copyright (c) 2005 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import os
+
+import zope.interface
+import zope.schema
+import zope.configuration.fields
+from zope.configuration.exceptions import ConfigurationError
+from zope.component import zcml
+from zope.publisher.interfaces.browser import IBrowserView
+from zope.publisher.interfaces.browser import IDefaultBrowserLayer
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+
+from z3c.macro import interfaces
+
+
+class IMacroDirective(zope.interface.Interface):
+ """Parameters for the template directive."""
+
+ template = zope.configuration.fields.Path(
+ title=u'Template defining a named macro.',
+ description=u"""Refers to a file containing a page template
+ (should end in extension ``.pt`` or ``.html``).
+ """,
+ required=True,
+ )
+
+ name = zope.schema.TextLine(
+ title=u'Name',
+ description=u"""
+ The macro name which this macro is registered for. The macro
+ name can be the same defined in metal:define-macro but does
+ not have to be the same. If no macro attribute is given the
+ name is used as the name defined in metal:define-macro. If you
+ need to register a macro under a different name as the defined
+ one, you can use the macro attribute which have to reference
+ the metal.define-macro name. The TALES expression calls macros
+ by this name and returns the macro within the same name or with
+ the name defined in the macro attribute.
+ """,
+ required=True,
+ default=u'',
+ )
+
+ macro = zope.schema.TextLine(
+ title=u'Macro',
+ description=u"""
+ The name of the macro to be used. This allows us to reference
+ the named macro defined with metal:define-macro if we use a
+ different IMacroDirective name.
+ """,
+ required=False,
+ default=u'',
+ )
+
+ for_ = zope.configuration.fields.GlobalObject(
+ title=u'Context',
+ description=u'The context for which the macro should be used',
+ required=False,
+ default=zope.interface.Interface,
+ )
+
+ view = zope.configuration.fields.GlobalObject(
+ title=u'View',
+ description=u'The view for which the macro should be used',
+ required=False,
+ default=IBrowserView)
+
+ layer = zope.configuration.fields.GlobalObject(
+ title=u'Layer',
+ description=u'The layer for which the macro should be used',
+ required=False,
+ default=IDefaultBrowserLayer,
+ )
+
+ contentType = zope.schema.BytesLine(
+ title=u'Content Type',
+ description=u'The content type identifies the type of data.',
+ default='text/html',
+ required=False,
+ )
+
+
+class MacroFactory(object):
+ """Macro factory."""
+
+ def __init__(self, path, macro, contentType):
+ self.path = path
+ self.macro = macro
+ self.contentType = contentType
+
+ def __call__(self, context, view, request):
+ template = ViewPageTemplateFile(self.path,
+ content_type=self.contentType)
+ return template.macros[self.macro]
+
+
+def registerMacroFactory(_context, path, name, macro, for_, view, layer,
+ contentType):
+ """Register a named macro factory adapter."""
+
+ factory = MacroFactory(path, macro, contentType)
+
+ # register the macro
+ zcml.adapter(_context, (factory,), interfaces.IMacroTemplate,
+ (for_, view, layer), name=name)
+
+
+def macroDirective(_context, template, name, macro=u'',
+ for_=zope.interface.Interface, view=IBrowserView,
+ layer=IDefaultBrowserLayer, contentType='text/html'):
+
+ # Make sure that the template exists
+ path = os.path.abspath(str(_context.path(template)))
+ if not os.path.isfile(path):
+ raise ConfigurationError("No such file", template)
+
+ if not macro:
+ macro = name
+
+ registerMacroFactory(_context, path, name, macro, for_, view, layer,
+ contentType)
Property changes on: z3c.macro/trunk/src/z3c/macro/zcml.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.macro/trunk/src/z3c/macro/zcml.txt
===================================================================
--- z3c.macro/trunk/src/z3c/macro/zcml.txt 2007-01-18 00:57:34 UTC (rev 72083)
+++ z3c.macro/trunk/src/z3c/macro/zcml.txt 2007-01-18 01:02:26 UTC (rev 72084)
@@ -0,0 +1,73 @@
+===============
+Macro directive
+===============
+
+A macro directive can be used for register macros. Take a look at the
+README.txt which explains the macro TALES expression.
+
+ >>> import sys
+ >>> from zope.configuration import xmlconfig
+ >>> import z3c.template
+ >>> context = xmlconfig.file('meta.zcml', z3c.macro)
+
+First define a template which defines a macro:
+
+ >>> import os, tempfile
+ >>> temp_dir = tempfile.mkdtemp()
+ >>> file = os.path.join(temp_dir, 'file.pt')
+ >>> open(file, 'w').write('''
+ ... <html>
+ ... <head>
+ ... <tal:block replace="structure provider:ITitle">
+ ... <metal:block define-macro="title">
+ ... <title>Pagelet skin</title>
+ ... </metal:block>
+ ... </tal:block>
+ ... </head>
+ ... <body>
+ ... <div>content</div>
+ ... </body>
+ ... </html>
+ ... ''')
+
+and register the macro provider within the ``z3c:macroProvider`` directive:
+
+ >>> context = xmlconfig.string("""
+ ... <configure
+ ... xmlns:z3c="http://namespaces.zope.org/z3c">
+ ... <z3c:macro
+ ... template="%s"
+ ... name="title"
+ ... />
+ ... </configure>
+ ... """ % file, context=context)
+
+We need a content object...
+
+ >>> import zope.interface
+ >>> class Content(object):
+ ... zope.interface.implements(zope.interface.Interface)
+ >>> content = Content()
+
+and we need a view...
+
+ >>> import zope.interface
+ >>> import zope.component
+ >>> from zope.publisher.browser import BrowserPage
+ >>> class View(BrowserPage):
+ ... def __init__(self, context, request):
+ ... self.context = context
+ ... self.request = request
+
+and we need a request:
+ >>> from zope.publisher.browser import TestRequest
+ >>> request = TestRequest()
+
+Check if we get the macro template:
+
+ >>> from z3c.macro import interfaces
+ >>> view = View(content, request)
+ >>> zope.component.getMultiAdapter((content, view, request),
+ ... interface=interfaces.IMacroTemplate, name='title')
+ [('version', '1.6'), ('mode', 'html'), ('setPosition', (5, 4)),...
+ [(u'define-macro', u'title', 'metal')]))]...
Property changes on: z3c.macro/trunk/src/z3c/macro/zcml.txt
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the Checkins
mailing list