/usr/share/pyshared/z3c/template/zcml.txt is in python-z3c.template 1.4.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | ==================
Template directive
==================
Show how we can use the template directive. Register the meta configuration for
the directive.
>>> import sys
>>> from zope.configuration import xmlconfig
>>> import z3c.template
>>> context = xmlconfig.file('meta.zcml', z3c.template)
PageTemplate
------------
We need a custom content template
>>> import os, tempfile
>>> temp_dir = tempfile.mkdtemp()
>>> file = os.path.join(temp_dir, 'content.pt')
>>> open(file, 'w').write('''<div>content</div>''')
and a interface
>>> import zope.interface
>>> class IView(zope.interface.Interface):
... """Marker interface"""
and a view class:
>>> from zope.publisher.browser import TestRequest
>>> class View(object):
... zope.interface.implements(IView)
... def __init__(self, context, request):
... self.context = context
... self.request = request
>>> request = TestRequest()
>>> view = View(object(), request)
Make them available under the fake package ``custom``:
>>> sys.modules['custom'] = type(
... 'Module', (),
... {'IView': IView})()
and register them as a template within the ``z3c:template`` directive:
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:template
... template="%s"
... for="custom.IView"
... />
... </configure>
... """ % file, context=context)
Let's get the template
>>> import zope.component
>>> from z3c.template.interfaces import IContentTemplate
>>> template = zope.component.queryMultiAdapter(
... (view, request),
... interface=IContentTemplate)
and check them:
>>> from z3c.template.template import ViewPageTemplateFile
>>> isinstance(template, ViewPageTemplateFile)
True
>>> print template(view)
<div>content</div>
Layout template
---------------
Define a layout template
>>> file = os.path.join(temp_dir, 'layout.pt')
>>> open(file, 'w').write('''<div>layout</div>''')
and register them as a layout template within the ``z3c:layout`` directive:
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:layout
... template="%s"
... for="custom.IView"
... />
... </configure>
... """ % file, context=context)
Let's get the template
>>> from z3c.template.interfaces import ILayoutTemplate
>>> layout = zope.component.queryMultiAdapter((view, request),
... interface=ILayoutTemplate)
and check them:
>>> isinstance(template, ViewPageTemplateFile)
True
>>> print layout(view)
<div>layout</div>
Context-specific template
-------------------------
Most of views have some object as their context and it's ofter very
useful to be able register context-specific template. We can do that
using the ``context`` argument of the ZCML directive.
Let's define some content type:
>>> class IContent(zope.interface.Interface):
... pass
>>> class Content(object):
... zope.interface.implements(IContent)
>>> sys.modules['custom'].IContent = IContent
Now, we can register a template for this class. Let's create one and
register:
>>> file = os.path.join(temp_dir, 'context.pt')
>>> open(file, 'w').write('''<div>i'm context-specific</div>''')
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:template
... template="%s"
... for="custom.IView"
... context="custom.IContent"
... />
... </configure>
... """ % file, context=context)
We can now lookup it using the (view, request, context) discriminator:
>>> content = Content()
>>> view = View(content, request)
>>> template = zope.component.queryMultiAdapter((view, request, content),
... interface=IContentTemplate)
>>> print template(view)
<div>i'm context-specific</div>
The same will work with layout registration directive:
>>> file = os.path.join(temp_dir, 'context_layout.pt')
>>> open(file, 'w').write('''<div>context-specific layout</div>''')
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:layout
... template="%s"
... for="custom.IView"
... context="custom.IContent"
... />
... </configure>
... """ % file, context=context)
>>> layout = zope.component.queryMultiAdapter((view, request, content),
... interface=ILayoutTemplate)
>>> print layout(view)
<div>context-specific layout</div>
Named template
--------------
Its possible to register template by name. Let us register a pagelet with the
name edit:
>>> editTemplate = os.path.join(temp_dir, 'edit.pt')
>>> open(editTemplate, 'w').write('''<div>edit</div>''')
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:template
... name="edit"
... template="%s"
... for="custom.IView"
... />
... </configure>
... """ % editTemplate, context=context)
And call it:
>>> from z3c.template.interfaces import ILayoutTemplate
>>> template = zope.component.queryMultiAdapter(
... (view, request),
... interface=IContentTemplate, name='edit')
>>> print template(view)
<div>edit</div>
Custom template
---------------
Or you can define own interfaces and register templates for them:
>>> from zope.pagetemplate.interfaces import IPageTemplate
>>> class IMyTemplate(IPageTemplate):
... """My template"""
Make the template interface available as a custom module class.
>>> sys.modules['custom'].IMyTemplate = IMyTemplate
Dfine a new template
>>> interfaceTemplate = os.path.join(temp_dir, 'interface.pt')
>>> open(interfaceTemplate, 'w').write('''<div>interface</div>''')
>>> context = xmlconfig.string("""
... <configure
... xmlns:z3c="http://namespaces.zope.org/z3c">
... <z3c:template
... template="%s"
... for="custom.IView"
... provides="custom.IMyTemplate"
... />
... </configure>
... """ % interfaceTemplate, context=context)
Let's see if we get the template by the new interface:
>>> from z3c.template.interfaces import ILayoutTemplate
>>> template = zope.component.queryMultiAdapter((view, request),
... interface=IMyTemplate,)
>>> print template(view)
<div>interface</div>
Cleanup
-------
Now we need to clean up the custom module.
>>> del sys.modules['custom']
|