==================
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, 'file.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:

  >>> template
  <zope.app.pagetemplate.viewpagetemplatefile.ViewPageTemplateFile object ...>

  >>> print template(view)
  <div>content</div>


Layout template
~~~~~~~~~~~~~~~

Define a layout template

  >>> file = os.path.join(temp_dir, 'file.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:

  >>> layout
  <zope.app.pagetemplate.viewpagetemplatefile.ViewPageTemplateFile object ...>

  >>> print layout(view)
  <div>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']
