Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install z3c.macroviewlet

How to install z3c.macroviewlet

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install z3c.macroviewlet
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.1.0 Available View build log
Windows (64-bit)
1.1.0 Available View build log
Mac OS X (10.5+)
1.1.0 Available View build log
Linux (32-bit)
1.1.0 Available View build log
Linux (64-bit)
1.1.0 Available View build log
 
License
ZPL 2.1
Depended by
Lastest release
version 1.1.0 on Jan 5th, 2011

Macro viewlets are Zope 3 UI components. In particular they allow the developer to specify viewlets based on macros instead of entire templates.

Macro Provider

This package provides a ZCML directive which allows you to register a macro defined in a template as a viewlet. Such a macro based viewlet acts 100% the same as a other viewlets. It could be very handy if you want to write a layout template in one page template and define selective parts as viewlets without adding any additional HTML. Let me show what this will look like:

The layout/master template can look like this:

<!DOCTYPE ...>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"

System Message: ERROR/3 (<string>, line 20)

Inconsistent literal block quoting.

i18n:domain="z3c"> <head> <tal:block replace="structure provider:ITitle"> <metal:block define-macro="title"> <title>The title</title> </metal:block> </tal:block> </head> <body tal:define="applicationURL request/getApplicationURL"> <div id="content"> <tal:block content="structure provider:pagelet">content</tal:block> </div> </div> </body> </html>

The tempalte above defines an ITitle provider which contains the definition for a macro within itself. You have to define a viewlet manager within the zope.viewlet ZCMl directive which provides ITitle as a viewlet manager. After that, you can register the template above as a layout template wthin the z3c:layout ZCML directive like this:

<z3c:layout

System Message: ERROR/3 (<string>, line 43)

Inconsistent literal block quoting.

for="*" layer="z3c.skin.pagelet.IPageletBrowserSkin" template="template.pt" />

Then you can register the macro viewlet for the ITitle viewlet manager like this:

<z3c:macroViewlet

System Message: ERROR/3 (<string>, line 52)

Inconsistent literal block quoting.

for="*" template="template.pt" macro="title" manager="z3c.skin.pagelet.ITitle" layer="z3c.skin.pagelet.IPageletBrowserSkin" />

As you can see, the ZCML configuration directive above uses title as the macro attribute and uses ITitle as the viewlet manager. This will use the following part of the template.pt:

<title>Pagelet skin</title>

and registers it as a viewlet. This viewlet gets rendered in the ITitle provider. As you can see, you can use a complete layout tempalte and use it as it is. And here it comes, you can offer an included viewlet manager rendering the viewlet which can be overriden for other contexts or views etc. You also can register more than one viewlet for the ITitle viewlet manager. Which of course makes no sense in our special title tag example.

Let's show this in some tests. We'll start by 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):

System Message: ERROR/3 (<string>, line 80)

Inconsistent literal block quoting.

... zope.interface.implements(zope.interface.Interface)

>>> content = Content()

We also create a temp dir for sample templates which will be defined later for testing:

>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()

And we register a security checker for the MacroViewlet class:

>>> from zope.configuration.xmlconfig import XMLConfig
>>> import zope.app.component
>>> import z3c.macroviewlet
>>> XMLConfig('meta.zcml', zope.app.component)()
>>> XMLConfig('configure.zcml', z3c.macroviewlet)()
Layout template

We define a template including a macro definition and using a provider:

>>> path = os.path.join(temp_dir, 'template.pt')
>>> open(path, 'w').write('''

System Message: ERROR/3 (<string>, line 106)

Inconsistent literal block quoting.

... <html> ... <body> ... <head> ... <tal:block replace="structure provider:ITitle"> ... <metal:block define-macro="title"> ... <title>The title</title> ... </metal:block> ... </tal:block> ... </head> ... <body tal:define="applicationURL request/getApplicationURL"> ... content ... </body> ... </html> ... ''')

Let's register a view class using the view template:

>>> import zope.interface
>>> from zope.app.pagetemplate import viewpagetemplatefile
>>> from zope.publisher.interfaces.browser import IBrowserView
>>> class View(object):

System Message: ERROR/3 (<string>, line 127)

Inconsistent literal block quoting.

... zope.interface.implements(IBrowserView) ... def __init__(self, context, request): ... self.context = context ... self.request = request ... def __call__(self): ... return viewpagetemplatefile.ViewPageTemplateFile(path)(self)

Let's prepare the view:

>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> view = View(content, request)

Let's define the viewlet manager ITitle:

>>> from zope.viewlet.interfaces import IViewletManager
>>> from zope.viewlet.manager import ViewletManager
>>> class ITitle(IViewletManager):

System Message: ERROR/3 (<string>, line 145)

Inconsistent literal block quoting.

... """Viewlet manager located in the title tag."""

>>> title = ViewletManager('title', ITitle)

Let's register the viewlet manager:

>>> from zope.viewlet.interfaces import IViewletManager
>>> manager = zope.component.provideAdapter(

System Message: ERROR/3 (<string>, line 153)

Inconsistent literal block quoting.

... title, ... (zope.interface.Interface, TestRequest, IBrowserView), ... IViewletManager, ... name='ITitle')

MacroViewlet

Before we register the macro viewlet, we check the rendered page without any registered macro viewlet:

>>> print view()

System Message: ERROR/3 (<string>, line 166)

Inconsistent literal block quoting.

<html> <body> <head> </head> <body> content </body> </body></html>

As you can see there is no title rendered. Now we can define the macro viewlet...:

>>> from zope.app.pagetemplate import viewpagetemplatefile
>>> from z3c.macroviewlet import zcml
>>> macroViewlet = zcml.MacroViewletFactory(path, 'title', 'text/html')

and register them as adapter:

>>> from zope.viewlet.interfaces import IViewlet
>>> zope.component.provideAdapter(

System Message: ERROR/3 (<string>, line 186)

Inconsistent literal block quoting.

... macroViewlet, ... (zope.interface.Interface, IDefaultBrowserLayer, IBrowserView, ... ITitle), ... IViewlet, ... name='title')

Now we are ready to test it again:

>>> print view()

System Message: ERROR/3 (<string>, line 195)

Inconsistent literal block quoting.

<html> <body> <head> <title>The title</title> </head> <body> content </body> </body></html>

As you can see, the title gets rendered as a viewlet into the ITitle provider.

Cleanup
>>> import shutil
>>> shutil.rmtree(temp_dir)

Changes

1.1.0 (2010-07-14)
  • Fixed tests to run with current package versions

System Message: WARNING/2 (<string>, line 223)

Bullet list ends without a blank line; unexpected unindent.

(metaconfigure.registerType moved from zope.app.pagetemplate to zope.browserpage). Thus requiring zope.browserpage now.

  • No longer use depreacted zope.testing.doctest and

System Message: WARNING/2 (<string>, line 227)

Bullet list ends without a blank line; unexpected unindent.

zope.testing.doctestunit.

  • Removed ZPKG and ZCML slugs.
  • Fixed RST formatting in README.
1.0.0 (2007-09-21)
  • Initial Release

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.