========== Directives ========== Widget template directive ------------------------- Show how we can use the widget template directive. Register the meta configuration for the directive. >>> import sys >>> from zope.configuration import xmlconfig >>> import z3c.form >>> context = xmlconfig.file('meta.zcml', z3c.form) We need a custom widget template >>> import os, tempfile >>> temp_dir = tempfile.mkdtemp() >>> file = os.path.join(temp_dir, 'widget.pt') >>> open(file, 'w').write(''' ... ... ... ... ''') and a interface >>> import zope.interface >>> from z3c.form import interfaces >>> class IMyWidget(interfaces.IWidget): ... """My widget interface.""" and a widget class: >>> from z3c.form.testing import TestRequest >>> from z3c.form.browser import text >>> class MyWidget(text.TextWidget): ... zope.interface.implements(IMyWidget) >>> request = TestRequest() >>> myWidget = MyWidget(request) Make them available under the fake package ``custom``: >>> sys.modules['custom'] = type( ... 'Module', (), ... {'IMyWidget': IMyWidget})() and register them as a widget template within the ``z3c:widgetTemplate`` directive: >>> context = xmlconfig.string(""" ... ... ... ... """ % file, context=context) Let's get the template >>> import zope.component >>> from z3c.template.interfaces import IPageTemplate >>> template = zope.component.queryMultiAdapter((None, request, None, None, ... myWidget), interface=IPageTemplate, name='input') and check it: >>> from z3c.form.ptcompat import ViewPageTemplateFile >>> isinstance(template, ViewPageTemplateFile) True Let's use the template within the widget. >>> print template(myWidget) We normally render the widget which returns the registered template. >>> print myWidget.render() If the template does not exist, then the widget directive should fail immediately: >>> unknownFile = os.path.join(temp_dir, 'unknown.pt') >>> context = xmlconfig.string(""" ... ... ... ... """ % unknownFile, context=context) Traceback (most recent call last): ... ZopeXMLConfigurationError: File "", line 4.2-7.8 ConfigurationError: ('No such file', '...unknown.pt') Object Widget template directive -------------------------------- Show how we can use the objectwidget template directive. The big difference between the 'simple' Widget template and the Object Widget directive is that the Object Widget template takes the field's schema into account. That makes it easy to register different widget templates for different sub-schemas. You can use this together with SubformAdapter to get a totally custom subwidget. We need a custom widget template >>> file = os.path.join(temp_dir, 'widget.pt') >>> open(file, 'w').write(''' ... ...
... yeah, this can get comlex ...
... ... ''') and a interface >>> class IMyObjectWidget(interfaces.IObjectWidget): ... """My objectwidget interface.""" and a widget class: >>> from z3c.form.browser import object >>> class MyObjectWidget(object.ObjectWidget): ... zope.interface.implements(IMyObjectWidget) >>> request = TestRequest() >>> myObjectWidget = MyObjectWidget(request) >>> from z3c.form.testing import IMySubObject >>> import zope.schema >>> field = zope.schema.Object( ... __name__='subobject', ... title=u'my object widget', ... schema=IMySubObject) >>> myObjectWidget.field = field Make them available under the fake package ``custom``: >>> sys.modules['custom'] = type( ... 'Module', (), ... {'IMyObjectWidget': IMyObjectWidget})() and register them as a widget template within the ``z3c:objectWidgetTemplate`` directive: >>> context = xmlconfig.string(""" ... ... ... ... """ % file, context=context) Let's get the template >>> template = zope.component.queryMultiAdapter((None, request, None, None, ... myObjectWidget, None), interface=IPageTemplate, name='input') and check it: >>> isinstance(template, ViewPageTemplateFile) True Let's use the template within the widget. >>> print template(myObjectWidget)
yeah, this can get comlex
We normally render the widget which returns the registered template. >>> print myObjectWidget.render()
yeah, this can get comlex
If the template does not exist, then the widget directive should fail immediately: >>> unknownFile = os.path.join(temp_dir, 'unknown.pt') >>> context = xmlconfig.string(""" ... ... ... ... """ % unknownFile, context=context) Traceback (most recent call last): ... ZopeXMLConfigurationError: File "", line 4.2-7.8 ConfigurationError: ('No such file', '...unknown.pt') Register a specific template for a schema: We need a custom widget template >>> file = os.path.join(temp_dir, 'widgetspec.pt') >>> open(file, 'w').write(''' ... ...
... this one is specific ...
... ... ''') >>> context = xmlconfig.string(""" ... ... ... ... """ % file, context=context) Let's get the template >>> template = zope.component.queryMultiAdapter((None, request, None, None, ... myObjectWidget, None), interface=IPageTemplate, name='input') and check it: >>> print myObjectWidget.render()
this one is specific
Cleanup ------- Now we need to clean up the custom module. >>> del sys.modules['custom'] Also let's not leave temporary files lying around >>> import shutil >>> shutil.rmtree(temp_dir)