z3c.pagelet


Namez3c.pagelet JSON
Version 3.1 PyPI version JSON
download
home_pagehttps://github.com/zopefoundation/z3c.pagelet
SummaryPagelets are way to specify a template without the O-wrap.
upload_time2024-10-21 07:01:49
maintainerNone
docs_urlNone
authorRoger Ineichen and the Zope Community
requires_python>=3.8
licenseZPL 2.1
keywords zope3 template pagelet layout zpt pagetemplate
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            .. image:: https://img.shields.io/pypi/v/z3c.pagelet.svg
        :target: https://pypi.python.org/pypi/z3c.pagelet/
        :alt: Latest release

.. image:: https://img.shields.io/pypi/pyversions/z3c.pagelet.svg
        :target: https://pypi.org/project/z3c.pagelet/
        :alt: Supported Python versions

.. image:: https://github.com/zopefoundation/z3c.pagelet/actions/workflows/tests.yml/badge.svg
        :target: https://github.com/zopefoundation/z3c.pagelet/actions/workflows/tests.yml


.. image:: https://coveralls.io/repos/github/zopefoundation/z3c.pagelet/badge.svg?branch=master
        :target: https://coveralls.io/github/zopefoundation/z3c.pagelet?branch=master



Pagelets are Zope 3 UI components. In particular they allow the developer to
specify content templates without worrying about the UI O-wrap.


========
Pagelets
========

.. contents::

This package provides a very flexible base implementation that can be used
to write view components which can be higly customized later in custom projects.
This is needed if you have to write reusable components like those needed
in a framework. Pagelets are BrowserPages made differently and can be used
to replace them.

What does this mean?

We separate the python view code from the template implementation. And we also
separate the template in at least two different templates - the content
template and the layout template.

This package uses z3c.template and offers an implementaton for this
template pattern. Additionaly this package offers a ``pagelet`` directive
wich can be used to register pagelets.

Pagelets are views which can be called and support the update and render
pattern.


How do they work
----------------

A pagelet returns the rendered content without layout in the render method and
returns the layout code if we call it. See also z3c.template which shows
how the template works. These samples will only show how the base implementation
located in the z3c.pagelet.browser module get used.


BrowserPagelet
--------------

The base implementation called BrowserPagelet offers builtin __call__ and
render methods which provide the different template lookups. Take a look at the
BrowserPagelet class located in z3c.pagelet.browser and you can see that the render
method returns a IContentTemplate and the __call__ method a ILayoutTemplate
defined in the z3c.layout package.

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

  >>> import zope.interface
  >>> import zope.component
  >>> from z3c.pagelet import interfaces
  >>> from z3c.pagelet import browser

We start by defining a page template rendering the pagelet content.

  >>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt')
  >>> with open(contentTemplate, 'w') as file:
  ...     _ = file.write('''
  ...   <div class="content">
  ...     my template content
  ...   </div>
  ... ''')

And we also define a layout template rendering the layout for a pagelet.
This template will call the render method from a pagelet:

  >>> layoutTemplate = os.path.join(temp_dir, 'layoutTemplate.pt')
  >>> with open(layoutTemplate, 'w') as file:
  ...     _ = file.write('''
  ...   <html>
  ...     <body>
  ...       <div class="layout" tal:content="structure view/render">
  ...         here comes the content
  ...       </div>
  ...     </body>
  ...   </html>
  ... ''')

Let's now register the template for the view and the request. We use the
TemplateFactory directly from the z3c.template package. This is commonly done
using the ZCML directive called ``z3c:template``. Note that we do use the
generic Interface as the view base interface to register the template. This
allows us to register a more specific template in the next sample:

  >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
  >>> from z3c.template.interfaces import IContentTemplate
  >>> from z3c.template.template import TemplateFactory
  >>> factory = TemplateFactory(contentTemplate, 'text/html')
  >>> zope.component.provideAdapter(
  ...     factory, (zope.interface.Interface, IDefaultBrowserLayer),
  ...     IContentTemplate)

And register the layout template using the ``Interface`` as registration base:

  >>> from z3c.template.interfaces import ILayoutTemplate
  >>> factory = TemplateFactory(layoutTemplate, 'text/html')
  >>> zope.component.provideAdapter(factory,
  ...     (zope.interface.Interface, IDefaultBrowserLayer), ILayoutTemplate)

Now define a view marker interface. Such a marker interface is used to let
us register our templates:

  >>> class IMyView(zope.interface.Interface):
  ...     pass

And we define a view class inherited from BrowserPagelet and implementing the
view marker interface:

  >>> @zope.interface.implementer(IMyView)
  ... class MyView(browser.BrowserPagelet):
  ...     pass

Now test the view class providing the view and check the output:

  >>> from zope.publisher.browser import TestRequest
  >>> request = TestRequest()
  >>> myView = MyView(root, request)
  >>> print(myView())
  <html>
    <body>
      <div class="layout">
        <div class="content">
          my template content
        </div>
      </div>
    </body>
  </html>

You can see the render method generates only the content:

  >>> print(myView.render())
  <div class="content">
    my template content
  </div>


Redirection
-----------

The pagelet doesn't bother rendering itself and its layout when request is a
redirection as the rendering doesn't make any sense with browser requests in
that case. Let's create a view that does a redirection in its update method.

  >>> class RedirectingView(MyView):
  ...     def update(self):
  ...         self.request.response.redirect('.')

It will return an empty string when called as a browser page.

  >>> redirectRequest = TestRequest()
  >>> redirectView = RedirectingView(root, redirectRequest)
  >>> redirectView() == ''
  True

However, the ``render`` method will render pagelet's template as usual:

  >>> print(redirectView.render())
  <div class="content">
    my template content
  </div>


PageletRenderer
---------------

There is also a standard pattern for calling the render method on pagelet.
Using the pagelet renderer which is a IContentProvider makes it possible to
reuse existing layout template without the pagelet. If you want to reuse a
layout template without a pagelet you simply have to provide another content
provider. It's flexible isn't it? As next let's show a sample using the
pagelet renderer.

We define a new layout template using the content provider called ```pagelet``


  >>> providerLayout = os.path.join(temp_dir, 'providerLayout.pt')
  >>> with open(providerLayout, 'w') as file:
  ...     _ = file.write('''
  ...   <html>
  ...     <body>
  ...       <div class="layout" tal:content="structure provider:pagelet">
  ...         here comes the content
  ...       </div>
  ...     </body>
  ...   </html>
  ... ''')

and register them. Now we use the specific interface defined in the view:

  >>> factory = TemplateFactory(providerLayout, 'text/html')
  >>> zope.component.provideAdapter(factory,
  ...     (zope.interface.Interface, IDefaultBrowserLayer), ILayoutTemplate)

Now let's call the view:

  >>> try:
  ...     myView()
  ... except Exception as e:
  ...     print(repr(e))
  ContentProviderLookupError('pagelet'...)

That's right, we need to register the content provider ``pagelet`` before we
can use it.

  >>> from zope.contentprovider.interfaces import IContentProvider
  >>> from z3c.pagelet import provider
  >>> zope.component.provideAdapter(provider.PageletRenderer,
  ...     provides=IContentProvider, name='pagelet')

Now let's call the view again:

  >>> print(myView())
  <html>
    <body>
      <div class="layout">
        <div class="content">
          my template content
        </div>
      </div>
    </body>
  </html>


Context-specific templates
--------------------------

Pagelets are also able to lookup templates using their context object
as an additional discriminator, via (self, self.request, self.context)
lookup. It's useful when you want to provide a custom template for
some specific content objects. Let's check that out.

First, let's define a custom content type and make an object to work with:

  >>> class IContent(zope.interface.Interface):
  ...     pass
  >>> @zope.interface.implementer(IContent)
  ... class Content(object):
  ...     pass

  >>> content = Content()

Let's use our view class we defined earlier. Currently, it will use
the layout and content templates we defined for (view, request) before:

  >>> myView = MyView(content, request)
  >>> print(myView())
  <html>
    <body>
      <div class="layout">
        <div class="content">
          my template content
        </div>
      </div>
    </body>
  </html>

Let's create context-specific layout and content templates and register
them for our IContent interface:

  >>> contextLayoutTemplate = os.path.join(temp_dir, 'contextLayoutTemplate.pt')
  >>> with open(contextLayoutTemplate, 'w') as file:
  ...     _ = file.write('''
  ...   <html>
  ...     <body>
  ...       <div class="context-layout" tal:content="structure provider:pagelet">
  ...         here comes the context-specific content
  ...       </div>
  ...     </body>
  ...   </html>
  ... ''')
  >>> factory = TemplateFactory(contextLayoutTemplate, 'text/html')
  >>> zope.component.provideAdapter(
  ...     factory, (zope.interface.Interface, IDefaultBrowserLayer, IContent),
  ...     ILayoutTemplate)

  >>> contextContentTemplate = os.path.join(temp_dir, 'contextContentTemplate.pt')
  >>> with open(contextContentTemplate, 'w') as file:
  ...     _ = file.write('''
  ...   <div class="context-content">
  ...     my context-specific template content
  ...   </div>
  ... ''')
  >>> factory = TemplateFactory(contextContentTemplate, 'text/html')
  >>> zope.component.provideAdapter(
  ...     factory, (zope.interface.Interface, IDefaultBrowserLayer, IContent),
  ...     IContentTemplate)

Now, our view should use context-specific templates for rendering:

  >>> print(myView())
  <html>
    <body>
      <div class="context-layout">
        <div class="context-content">
          my context-specific template content
        </div>
      </div>
    </body>
  </html>


Add, Edit and Display forms (formlib)
-------------------------------------

What would the pagelet be without any formlib based implementations?
We offer base implementations for add, edit and display forms based on
the formlib. **Note:** To make sure these classes get defined, you
should have ``zope.formlib`` already installed, as ``z3c.pagelet``
does not directly depend on ``zope.formlib`` because there are other
form libraries.

For the next tests we provide a generic form template
like those used in formlib. This template is registered within this package
as a default for the formlib based mixin classes:

  >>> from z3c import pagelet
  >>> baseDir = os.path.split(pagelet.__file__)[0]
  >>> formTemplate = os.path.join(baseDir, 'form.pt')
  >>> factory = TemplateFactory(formTemplate, 'text/html')
  >>> zope.component.provideAdapter(
  ...     factory,
  ...     (interfaces.IPageletForm, IDefaultBrowserLayer), IContentTemplate)

And we define a new interface including a text attribute:

  >>> import zope.schema
  >>> class IDocument(zope.interface.Interface):
  ...     """A document."""
  ...     text = zope.schema.TextLine(title=u'Text', description=u'Text attr.')

Also define a content object which implements the interface:

  >>> @zope.interface.implementer(IDocument)
  ... class Document(object):
  ...     text = None
  >>> document = Document()

PageletAddForm
~~~~~~~~~~~~~~

Now let's define an add from based on the PageletAddForm class:

  >>> from zope.formlib import form
  >>> class MyAddForm(browser.PageletAddForm):
  ...     form_fields = form.Fields(IDocument)
  ...     def createAndAdd(self, data):
  ...         title = data.get('title', u'')
  ...         doc = Document()
  ...         doc.title = title
  ...         root['document'] = doc
  ...         return doc

Now render the form:

  >>> addForm = MyAddForm(root, request)
  >>> print(addForm())
  <html>
    <body>
      <div class="layout">
        <form action="http://127.0.0.1" method="post"
              enctype="multipart/form-data" class="edit-form"
              id="zc.page.browser_form">
          <table class="form-fields">
            <tr>
              <td class="label">
                <label for="form.text">
                <span class="required">*</span><span>Text</span>
                </label>
              </td>
              <td class="field">
                <div class="form-fields-help"
                     id="field-help-for-form.text">Text attr.</div>
                <div class="widget"><input class="textType" id="form.text"
                     name="form.text" size="20" type="text" value=""  /></div>
              </td>
            </tr>
          </table>
        <div class="form-controls">
          <input type="submit" id="form.actions.add" name="form.actions.add"
                 value="Add" class="button" />
        </div>
      </form>
    </div>
    </body>
  </html>


PageletEditForm
~~~~~~~~~~~~~~~

Now let's define an edit form based on the PageletEditForm class:

  >>> class MyEditForm(browser.PageletEditForm):
  ...     form_fields = form.Fields(IDocument)

and render the form:

  >>> document.text = u'foo'
  >>> editForm = MyEditForm(document, request)
  >>> print(editForm())
  <html>
    <body>
      <div class="layout">
        <form action="http://127.0.0.1" method="post"
              enctype="multipart/form-data" class="edit-form"
              id="zc.page.browser_form">
          <table class="form-fields">
              <tr>
                <td class="label">
                  <label for="form.text">
                  <span class="required">*</span><span>Text</span>
                  </label>
                </td>
                <td class="field">
                  <div class="form-fields-help"
                       id="field-help-for-form.text">Text attr.</div>
                  <div class="widget"><input class="textType" id="form.text"
                       name="form.text" size="20" type="text" value="foo"
                       /></div>
                </td>
              </tr>
          </table>
          <div class="form-controls">
            <input type="submit" id="form.actions.apply"
                   name="form.actions.apply" value="Apply" class="button" />
          </div>
        </form>
      </div>
    </body>
  </html>


PageletDisplayForm
~~~~~~~~~~~~~~~~~~

Now let's define a display form based on the PageletDisplayForm class...

  >>> class MyDisplayForm(browser.PageletDisplayForm):
  ...     form_fields = form.Fields(IDocument)

and render the form:

  >>> document.text = u'foo'
  >>> displayForm = MyDisplayForm(document, request)
  >>> print(displayForm())
  <html>
    <body>
      <div class="layout">
        <form action="http://127.0.0.1" method="post"
              enctype="multipart/form-data" class="edit-form"
              id="zc.page.browser_form">
          <table class="form-fields">
              <tr>
                <td class="label">
                  <label for="form.text">
                  <span>Text</span>
                  </label>
                </td>
                <td class="field">
                  <div class="form-fields-help"
                       id="field-help-for-form.text">Text attr.</div>
                  <div class="widget">foo</div>
                </td>
              </tr>
          </table>
        </form>
      </div>
    </body>
  </html>


Cleanup
-------

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


=======
CHANGES
=======

3.1 (2024-10-21)
----------------

- Add support for Python 3.12, 3.13.

- Drop support for Python 3.7.


3.0 (2023-02-09)
----------------

- Drop support for Python 2.7, 3.4, 3.5, 3.6.

- Add support for Python 3.8, 3.9, 3.10, 3.11.


2.1 (2018-11-12)
----------------

- Claim support for Python 3.5, 3.6, 3.7, PyPy and PyPy3.

- Drop support for Python 2.6 and 3.3.

- Drop support for ``python setup.py test``.


2.0.0 (2015-11-09)
------------------

- Standardize namespace __init__.

- Claim support for Python 3.4.


2.0.0a1 (2013-02-28)
--------------------

- Added support for Python 3.3.

- Replaced deprecated ``zope.interface.implements`` usage with equivalent
  ``zope.interface.implementer`` decorator.

- Dropped support for Python 2.4 and 2.5.


1.3.1 (2012-09-15)
------------------

- Fix ``IPageletDirective`` after a change in
  ``zope.component.zcml.IBasicViewInformation``


1.3.0 (2011-10-29)
------------------

- Moved z3c.pt include to extras_require chameleon. This makes the package
  independent from chameleon and friends and allows to include this
  dependencies in your own project.

- Upgrade to chameleon 2.0 template engine and use the newest z3c.pt and
  z3c.ptcompat packages adjusted to work with chameleon 2.0.

  See the notes from the z3c.ptcompat package:

  Update z3c.ptcompat implementation to use component-based template engine
  configuration, plugging directly into the Zope Toolkit framework.

  The z3c.ptcompat package no longer provides template classes, or ZCML
  directives; you should import directly from the ZTK codebase.

  Note that the ``PREFER_Z3C_PT`` environment option has been
  rendered obsolete; instead, this is now managed via component
  configuration.

  Also note that the chameleon CHAMELEON_CACHE environment value changed from
  True/False to a path. Skip this property if you don't like to use a cache.
  None or False defined in buildout environment section doesn't work. At least
  with chameleon <= 2.5.4

  Attention: You need to include the configure.zcml file from z3c.ptcompat
  for enable the z3c.pt template engine. The configure.zcml will plugin the
  template engine. Also remove any custom built hooks which will import
  z3c.ptcompat in your tests or other places.


1.2.2 (2011-08-18)
------------------

- Change request interface in pagelet adapter signature e.g.
  (context, request, pagelet). Switch from IBrowserRequest to IHTTPRequest.
  This allows to use the pagelet pattern for jsonrpc request which doesn't
  provide IBrowserRequest. Also reflect the changes in configure.zcml


1.2.1 (2010-07-29)
------------------

- ``zope.app.pagetemplate.metaconfigure.registerType`` was moved to
  ``zope.browserpage``, so it gets now imported from there.

- Dropped test dependency on ``zope.app.security``, it is no longer
  needed.

- Using python's ``doctest`` instead of deprecated
  ``zope.testing.doctest[unit]``.


1.2.0 (2009-08-27)
------------------

- Fix untrusted redirect to google.com in tests. It's now forbidden by default
  by newer zope.publisher versions.

- Change ``zope.app.publisher`` dependency to new ``zope.browserpage``, as it
  has much less dependencies.

1.1.0 (2009-05-28)
------------------

* Got rid of dependency on ``zope.app.component`` by requiring
  ``zope.component >= 3.7``.

* Removed hard dependency on ``zope.formlib``: the pagelet forms now
  only get defined when ``zope.formlib`` is available. Tests still
  depend on ``zope.formlib``, so it got a test dependency.

* Made sure long_description renders fine on pypi.


1.0.3 (2009-02-27)
------------------

* Allow use of ``z3c.pt`` using ``z3c.ptcompat`` compatibility layer.

* Add support for context-specific layout and content template lookup,
  using (view, request, context) discriminator. This is compatible with
  context-specific templates introduced in z3c.template 1.2.0.

* Don't do rendering in pagelet's __call__ method when request is a redirection.

* Add sphinx-based HTML documentation building part to the buildout.


1.0.2 (2008-01-21)
------------------

* Added a `form.zcml` which can be included to have a template for
  ``PageletAddForm``, ``PageletEditForm`` and ``PageletDisplayForm``.


1.0.1 (2007-10-08)
------------------

* Added ``update()`` and ``render()`` method to ``IPagelet`` which was
  not specified but used.

* Fixed a infinite recursion bug when a layout template was registered for "*"
  but no content template was registered for a pagelet.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zopefoundation/z3c.pagelet",
    "name": "z3c.pagelet",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "zope3 template pagelet layout zpt pagetemplate",
    "author": "Roger Ineichen and the Zope Community",
    "author_email": "zope-dev@zope.dev",
    "download_url": "https://files.pythonhosted.org/packages/55/ac/0720cf56d9dd38870ac614879ad657c69c9b042dde49a7f3d08dfd3356f0/z3c.pagelet-3.1.tar.gz",
    "platform": null,
    "description": ".. image:: https://img.shields.io/pypi/v/z3c.pagelet.svg\n        :target: https://pypi.python.org/pypi/z3c.pagelet/\n        :alt: Latest release\n\n.. image:: https://img.shields.io/pypi/pyversions/z3c.pagelet.svg\n        :target: https://pypi.org/project/z3c.pagelet/\n        :alt: Supported Python versions\n\n.. image:: https://github.com/zopefoundation/z3c.pagelet/actions/workflows/tests.yml/badge.svg\n        :target: https://github.com/zopefoundation/z3c.pagelet/actions/workflows/tests.yml\n\n\n.. image:: https://coveralls.io/repos/github/zopefoundation/z3c.pagelet/badge.svg?branch=master\n        :target: https://coveralls.io/github/zopefoundation/z3c.pagelet?branch=master\n\n\n\nPagelets are Zope 3 UI components. In particular they allow the developer to\nspecify content templates without worrying about the UI O-wrap.\n\n\n========\nPagelets\n========\n\n.. contents::\n\nThis package provides a very flexible base implementation that can be used\nto write view components which can be higly customized later in custom projects.\nThis is needed if you have to write reusable components like those needed\nin a framework. Pagelets are BrowserPages made differently and can be used\nto replace them.\n\nWhat does this mean?\n\nWe separate the python view code from the template implementation. And we also\nseparate the template in at least two different templates - the content\ntemplate and the layout template.\n\nThis package uses z3c.template and offers an implementaton for this\ntemplate pattern. Additionaly this package offers a ``pagelet`` directive\nwich can be used to register pagelets.\n\nPagelets are views which can be called and support the update and render\npattern.\n\n\nHow do they work\n----------------\n\nA pagelet returns the rendered content without layout in the render method and\nreturns the layout code if we call it. See also z3c.template which shows\nhow the template works. These samples will only show how the base implementation\nlocated in the z3c.pagelet.browser module get used.\n\n\nBrowserPagelet\n--------------\n\nThe base implementation called BrowserPagelet offers builtin __call__ and\nrender methods which provide the different template lookups. Take a look at the\nBrowserPagelet class located in z3c.pagelet.browser and you can see that the render\nmethod returns a IContentTemplate and the __call__ method a ILayoutTemplate\ndefined in the z3c.layout package.\n\n  >>> import os, tempfile\n  >>> temp_dir = tempfile.mkdtemp()\n\n  >>> import zope.interface\n  >>> import zope.component\n  >>> from z3c.pagelet import interfaces\n  >>> from z3c.pagelet import browser\n\nWe start by defining a page template rendering the pagelet content.\n\n  >>> contentTemplate = os.path.join(temp_dir, 'contentTemplate.pt')\n  >>> with open(contentTemplate, 'w') as file:\n  ...     _ = file.write('''\n  ...   <div class=\"content\">\n  ...     my template content\n  ...   </div>\n  ... ''')\n\nAnd we also define a layout template rendering the layout for a pagelet.\nThis template will call the render method from a pagelet:\n\n  >>> layoutTemplate = os.path.join(temp_dir, 'layoutTemplate.pt')\n  >>> with open(layoutTemplate, 'w') as file:\n  ...     _ = file.write('''\n  ...   <html>\n  ...     <body>\n  ...       <div class=\"layout\" tal:content=\"structure view/render\">\n  ...         here comes the content\n  ...       </div>\n  ...     </body>\n  ...   </html>\n  ... ''')\n\nLet's now register the template for the view and the request. We use the\nTemplateFactory directly from the z3c.template package. This is commonly done\nusing the ZCML directive called ``z3c:template``. Note that we do use the\ngeneric Interface as the view base interface to register the template. This\nallows us to register a more specific template in the next sample:\n\n  >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer\n  >>> from z3c.template.interfaces import IContentTemplate\n  >>> from z3c.template.template import TemplateFactory\n  >>> factory = TemplateFactory(contentTemplate, 'text/html')\n  >>> zope.component.provideAdapter(\n  ...     factory, (zope.interface.Interface, IDefaultBrowserLayer),\n  ...     IContentTemplate)\n\nAnd register the layout template using the ``Interface`` as registration base:\n\n  >>> from z3c.template.interfaces import ILayoutTemplate\n  >>> factory = TemplateFactory(layoutTemplate, 'text/html')\n  >>> zope.component.provideAdapter(factory,\n  ...     (zope.interface.Interface, IDefaultBrowserLayer), ILayoutTemplate)\n\nNow define a view marker interface. Such a marker interface is used to let\nus register our templates:\n\n  >>> class IMyView(zope.interface.Interface):\n  ...     pass\n\nAnd we define a view class inherited from BrowserPagelet and implementing the\nview marker interface:\n\n  >>> @zope.interface.implementer(IMyView)\n  ... class MyView(browser.BrowserPagelet):\n  ...     pass\n\nNow test the view class providing the view and check the output:\n\n  >>> from zope.publisher.browser import TestRequest\n  >>> request = TestRequest()\n  >>> myView = MyView(root, request)\n  >>> print(myView())\n  <html>\n    <body>\n      <div class=\"layout\">\n        <div class=\"content\">\n          my template content\n        </div>\n      </div>\n    </body>\n  </html>\n\nYou can see the render method generates only the content:\n\n  >>> print(myView.render())\n  <div class=\"content\">\n    my template content\n  </div>\n\n\nRedirection\n-----------\n\nThe pagelet doesn't bother rendering itself and its layout when request is a\nredirection as the rendering doesn't make any sense with browser requests in\nthat case. Let's create a view that does a redirection in its update method.\n\n  >>> class RedirectingView(MyView):\n  ...     def update(self):\n  ...         self.request.response.redirect('.')\n\nIt will return an empty string when called as a browser page.\n\n  >>> redirectRequest = TestRequest()\n  >>> redirectView = RedirectingView(root, redirectRequest)\n  >>> redirectView() == ''\n  True\n\nHowever, the ``render`` method will render pagelet's template as usual:\n\n  >>> print(redirectView.render())\n  <div class=\"content\">\n    my template content\n  </div>\n\n\nPageletRenderer\n---------------\n\nThere is also a standard pattern for calling the render method on pagelet.\nUsing the pagelet renderer which is a IContentProvider makes it possible to\nreuse existing layout template without the pagelet. If you want to reuse a\nlayout template without a pagelet you simply have to provide another content\nprovider. It's flexible isn't it? As next let's show a sample using the\npagelet renderer.\n\nWe define a new layout template using the content provider called ```pagelet``\n\n\n  >>> providerLayout = os.path.join(temp_dir, 'providerLayout.pt')\n  >>> with open(providerLayout, 'w') as file:\n  ...     _ = file.write('''\n  ...   <html>\n  ...     <body>\n  ...       <div class=\"layout\" tal:content=\"structure provider:pagelet\">\n  ...         here comes the content\n  ...       </div>\n  ...     </body>\n  ...   </html>\n  ... ''')\n\nand register them. Now we use the specific interface defined in the view:\n\n  >>> factory = TemplateFactory(providerLayout, 'text/html')\n  >>> zope.component.provideAdapter(factory,\n  ...     (zope.interface.Interface, IDefaultBrowserLayer), ILayoutTemplate)\n\nNow let's call the view:\n\n  >>> try:\n  ...     myView()\n  ... except Exception as e:\n  ...     print(repr(e))\n  ContentProviderLookupError('pagelet'...)\n\nThat's right, we need to register the content provider ``pagelet`` before we\ncan use it.\n\n  >>> from zope.contentprovider.interfaces import IContentProvider\n  >>> from z3c.pagelet import provider\n  >>> zope.component.provideAdapter(provider.PageletRenderer,\n  ...     provides=IContentProvider, name='pagelet')\n\nNow let's call the view again:\n\n  >>> print(myView())\n  <html>\n    <body>\n      <div class=\"layout\">\n        <div class=\"content\">\n          my template content\n        </div>\n      </div>\n    </body>\n  </html>\n\n\nContext-specific templates\n--------------------------\n\nPagelets are also able to lookup templates using their context object\nas an additional discriminator, via (self, self.request, self.context)\nlookup. It's useful when you want to provide a custom template for\nsome specific content objects. Let's check that out.\n\nFirst, let's define a custom content type and make an object to work with:\n\n  >>> class IContent(zope.interface.Interface):\n  ...     pass\n  >>> @zope.interface.implementer(IContent)\n  ... class Content(object):\n  ...     pass\n\n  >>> content = Content()\n\nLet's use our view class we defined earlier. Currently, it will use\nthe layout and content templates we defined for (view, request) before:\n\n  >>> myView = MyView(content, request)\n  >>> print(myView())\n  <html>\n    <body>\n      <div class=\"layout\">\n        <div class=\"content\">\n          my template content\n        </div>\n      </div>\n    </body>\n  </html>\n\nLet's create context-specific layout and content templates and register\nthem for our IContent interface:\n\n  >>> contextLayoutTemplate = os.path.join(temp_dir, 'contextLayoutTemplate.pt')\n  >>> with open(contextLayoutTemplate, 'w') as file:\n  ...     _ = file.write('''\n  ...   <html>\n  ...     <body>\n  ...       <div class=\"context-layout\" tal:content=\"structure provider:pagelet\">\n  ...         here comes the context-specific content\n  ...       </div>\n  ...     </body>\n  ...   </html>\n  ... ''')\n  >>> factory = TemplateFactory(contextLayoutTemplate, 'text/html')\n  >>> zope.component.provideAdapter(\n  ...     factory, (zope.interface.Interface, IDefaultBrowserLayer, IContent),\n  ...     ILayoutTemplate)\n\n  >>> contextContentTemplate = os.path.join(temp_dir, 'contextContentTemplate.pt')\n  >>> with open(contextContentTemplate, 'w') as file:\n  ...     _ = file.write('''\n  ...   <div class=\"context-content\">\n  ...     my context-specific template content\n  ...   </div>\n  ... ''')\n  >>> factory = TemplateFactory(contextContentTemplate, 'text/html')\n  >>> zope.component.provideAdapter(\n  ...     factory, (zope.interface.Interface, IDefaultBrowserLayer, IContent),\n  ...     IContentTemplate)\n\nNow, our view should use context-specific templates for rendering:\n\n  >>> print(myView())\n  <html>\n    <body>\n      <div class=\"context-layout\">\n        <div class=\"context-content\">\n          my context-specific template content\n        </div>\n      </div>\n    </body>\n  </html>\n\n\nAdd, Edit and Display forms (formlib)\n-------------------------------------\n\nWhat would the pagelet be without any formlib based implementations?\nWe offer base implementations for add, edit and display forms based on\nthe formlib. **Note:** To make sure these classes get defined, you\nshould have ``zope.formlib`` already installed, as ``z3c.pagelet``\ndoes not directly depend on ``zope.formlib`` because there are other\nform libraries.\n\nFor the next tests we provide a generic form template\nlike those used in formlib. This template is registered within this package\nas a default for the formlib based mixin classes:\n\n  >>> from z3c import pagelet\n  >>> baseDir = os.path.split(pagelet.__file__)[0]\n  >>> formTemplate = os.path.join(baseDir, 'form.pt')\n  >>> factory = TemplateFactory(formTemplate, 'text/html')\n  >>> zope.component.provideAdapter(\n  ...     factory,\n  ...     (interfaces.IPageletForm, IDefaultBrowserLayer), IContentTemplate)\n\nAnd we define a new interface including a text attribute:\n\n  >>> import zope.schema\n  >>> class IDocument(zope.interface.Interface):\n  ...     \"\"\"A document.\"\"\"\n  ...     text = zope.schema.TextLine(title=u'Text', description=u'Text attr.')\n\nAlso define a content object which implements the interface:\n\n  >>> @zope.interface.implementer(IDocument)\n  ... class Document(object):\n  ...     text = None\n  >>> document = Document()\n\nPageletAddForm\n~~~~~~~~~~~~~~\n\nNow let's define an add from based on the PageletAddForm class:\n\n  >>> from zope.formlib import form\n  >>> class MyAddForm(browser.PageletAddForm):\n  ...     form_fields = form.Fields(IDocument)\n  ...     def createAndAdd(self, data):\n  ...         title = data.get('title', u'')\n  ...         doc = Document()\n  ...         doc.title = title\n  ...         root['document'] = doc\n  ...         return doc\n\nNow render the form:\n\n  >>> addForm = MyAddForm(root, request)\n  >>> print(addForm())\n  <html>\n    <body>\n      <div class=\"layout\">\n        <form action=\"http://127.0.0.1\" method=\"post\"\n              enctype=\"multipart/form-data\" class=\"edit-form\"\n              id=\"zc.page.browser_form\">\n          <table class=\"form-fields\">\n            <tr>\n              <td class=\"label\">\n                <label for=\"form.text\">\n                <span class=\"required\">*</span><span>Text</span>\n                </label>\n              </td>\n              <td class=\"field\">\n                <div class=\"form-fields-help\"\n                     id=\"field-help-for-form.text\">Text attr.</div>\n                <div class=\"widget\"><input class=\"textType\" id=\"form.text\"\n                     name=\"form.text\" size=\"20\" type=\"text\" value=\"\"  /></div>\n              </td>\n            </tr>\n          </table>\n        <div class=\"form-controls\">\n          <input type=\"submit\" id=\"form.actions.add\" name=\"form.actions.add\"\n                 value=\"Add\" class=\"button\" />\n        </div>\n      </form>\n    </div>\n    </body>\n  </html>\n\n\nPageletEditForm\n~~~~~~~~~~~~~~~\n\nNow let's define an edit form based on the PageletEditForm class:\n\n  >>> class MyEditForm(browser.PageletEditForm):\n  ...     form_fields = form.Fields(IDocument)\n\nand render the form:\n\n  >>> document.text = u'foo'\n  >>> editForm = MyEditForm(document, request)\n  >>> print(editForm())\n  <html>\n    <body>\n      <div class=\"layout\">\n        <form action=\"http://127.0.0.1\" method=\"post\"\n              enctype=\"multipart/form-data\" class=\"edit-form\"\n              id=\"zc.page.browser_form\">\n          <table class=\"form-fields\">\n              <tr>\n                <td class=\"label\">\n                  <label for=\"form.text\">\n                  <span class=\"required\">*</span><span>Text</span>\n                  </label>\n                </td>\n                <td class=\"field\">\n                  <div class=\"form-fields-help\"\n                       id=\"field-help-for-form.text\">Text attr.</div>\n                  <div class=\"widget\"><input class=\"textType\" id=\"form.text\"\n                       name=\"form.text\" size=\"20\" type=\"text\" value=\"foo\"\n                       /></div>\n                </td>\n              </tr>\n          </table>\n          <div class=\"form-controls\">\n            <input type=\"submit\" id=\"form.actions.apply\"\n                   name=\"form.actions.apply\" value=\"Apply\" class=\"button\" />\n          </div>\n        </form>\n      </div>\n    </body>\n  </html>\n\n\nPageletDisplayForm\n~~~~~~~~~~~~~~~~~~\n\nNow let's define a display form based on the PageletDisplayForm class...\n\n  >>> class MyDisplayForm(browser.PageletDisplayForm):\n  ...     form_fields = form.Fields(IDocument)\n\nand render the form:\n\n  >>> document.text = u'foo'\n  >>> displayForm = MyDisplayForm(document, request)\n  >>> print(displayForm())\n  <html>\n    <body>\n      <div class=\"layout\">\n        <form action=\"http://127.0.0.1\" method=\"post\"\n              enctype=\"multipart/form-data\" class=\"edit-form\"\n              id=\"zc.page.browser_form\">\n          <table class=\"form-fields\">\n              <tr>\n                <td class=\"label\">\n                  <label for=\"form.text\">\n                  <span>Text</span>\n                  </label>\n                </td>\n                <td class=\"field\">\n                  <div class=\"form-fields-help\"\n                       id=\"field-help-for-form.text\">Text attr.</div>\n                  <div class=\"widget\">foo</div>\n                </td>\n              </tr>\n          </table>\n        </form>\n      </div>\n    </body>\n  </html>\n\n\nCleanup\n-------\n\n  >>> import shutil\n  >>> shutil.rmtree(temp_dir)\n\n\n=======\nCHANGES\n=======\n\n3.1 (2024-10-21)\n----------------\n\n- Add support for Python 3.12, 3.13.\n\n- Drop support for Python 3.7.\n\n\n3.0 (2023-02-09)\n----------------\n\n- Drop support for Python 2.7, 3.4, 3.5, 3.6.\n\n- Add support for Python 3.8, 3.9, 3.10, 3.11.\n\n\n2.1 (2018-11-12)\n----------------\n\n- Claim support for Python 3.5, 3.6, 3.7, PyPy and PyPy3.\n\n- Drop support for Python 2.6 and 3.3.\n\n- Drop support for ``python setup.py test``.\n\n\n2.0.0 (2015-11-09)\n------------------\n\n- Standardize namespace __init__.\n\n- Claim support for Python 3.4.\n\n\n2.0.0a1 (2013-02-28)\n--------------------\n\n- Added support for Python 3.3.\n\n- Replaced deprecated ``zope.interface.implements`` usage with equivalent\n  ``zope.interface.implementer`` decorator.\n\n- Dropped support for Python 2.4 and 2.5.\n\n\n1.3.1 (2012-09-15)\n------------------\n\n- Fix ``IPageletDirective`` after a change in\n  ``zope.component.zcml.IBasicViewInformation``\n\n\n1.3.0 (2011-10-29)\n------------------\n\n- Moved z3c.pt include to extras_require chameleon. This makes the package\n  independent from chameleon and friends and allows to include this\n  dependencies in your own project.\n\n- Upgrade to chameleon 2.0 template engine and use the newest z3c.pt and\n  z3c.ptcompat packages adjusted to work with chameleon 2.0.\n\n  See the notes from the z3c.ptcompat package:\n\n  Update z3c.ptcompat implementation to use component-based template engine\n  configuration, plugging directly into the Zope Toolkit framework.\n\n  The z3c.ptcompat package no longer provides template classes, or ZCML\n  directives; you should import directly from the ZTK codebase.\n\n  Note that the ``PREFER_Z3C_PT`` environment option has been\n  rendered obsolete; instead, this is now managed via component\n  configuration.\n\n  Also note that the chameleon CHAMELEON_CACHE environment value changed from\n  True/False to a path. Skip this property if you don't like to use a cache.\n  None or False defined in buildout environment section doesn't work. At least\n  with chameleon <= 2.5.4\n\n  Attention: You need to include the configure.zcml file from z3c.ptcompat\n  for enable the z3c.pt template engine. The configure.zcml will plugin the\n  template engine. Also remove any custom built hooks which will import\n  z3c.ptcompat in your tests or other places.\n\n\n1.2.2 (2011-08-18)\n------------------\n\n- Change request interface in pagelet adapter signature e.g.\n  (context, request, pagelet). Switch from IBrowserRequest to IHTTPRequest.\n  This allows to use the pagelet pattern for jsonrpc request which doesn't\n  provide IBrowserRequest. Also reflect the changes in configure.zcml\n\n\n1.2.1 (2010-07-29)\n------------------\n\n- ``zope.app.pagetemplate.metaconfigure.registerType`` was moved to\n  ``zope.browserpage``, so it gets now imported from there.\n\n- Dropped test dependency on ``zope.app.security``, it is no longer\n  needed.\n\n- Using python's ``doctest`` instead of deprecated\n  ``zope.testing.doctest[unit]``.\n\n\n1.2.0 (2009-08-27)\n------------------\n\n- Fix untrusted redirect to google.com in tests. It's now forbidden by default\n  by newer zope.publisher versions.\n\n- Change ``zope.app.publisher`` dependency to new ``zope.browserpage``, as it\n  has much less dependencies.\n\n1.1.0 (2009-05-28)\n------------------\n\n* Got rid of dependency on ``zope.app.component`` by requiring\n  ``zope.component >= 3.7``.\n\n* Removed hard dependency on ``zope.formlib``: the pagelet forms now\n  only get defined when ``zope.formlib`` is available. Tests still\n  depend on ``zope.formlib``, so it got a test dependency.\n\n* Made sure long_description renders fine on pypi.\n\n\n1.0.3 (2009-02-27)\n------------------\n\n* Allow use of ``z3c.pt`` using ``z3c.ptcompat`` compatibility layer.\n\n* Add support for context-specific layout and content template lookup,\n  using (view, request, context) discriminator. This is compatible with\n  context-specific templates introduced in z3c.template 1.2.0.\n\n* Don't do rendering in pagelet's __call__ method when request is a redirection.\n\n* Add sphinx-based HTML documentation building part to the buildout.\n\n\n1.0.2 (2008-01-21)\n------------------\n\n* Added a `form.zcml` which can be included to have a template for\n  ``PageletAddForm``, ``PageletEditForm`` and ``PageletDisplayForm``.\n\n\n1.0.1 (2007-10-08)\n------------------\n\n* Added ``update()`` and ``render()`` method to ``IPagelet`` which was\n  not specified but used.\n\n* Fixed a infinite recursion bug when a layout template was registered for \"*\"\n  but no content template was registered for a pagelet.\n",
    "bugtrack_url": null,
    "license": "ZPL 2.1",
    "summary": "Pagelets are way to specify a template without the O-wrap.",
    "version": "3.1",
    "project_urls": {
        "Homepage": "https://github.com/zopefoundation/z3c.pagelet"
    },
    "split_keywords": [
        "zope3",
        "template",
        "pagelet",
        "layout",
        "zpt",
        "pagetemplate"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fa3fac70377b4d33f50d09f284341e885b49d3d1ccf26343e39f030041d348a1",
                "md5": "e2dc0429b8cdfccdc2107efaf9b264fc",
                "sha256": "f31845d0027b844c0811f67924352d94c831541b3a8aeb2d3f544f57360f6ad6"
            },
            "downloads": -1,
            "filename": "z3c.pagelet-3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e2dc0429b8cdfccdc2107efaf9b264fc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 24632,
            "upload_time": "2024-10-21T07:01:47",
            "upload_time_iso_8601": "2024-10-21T07:01:47.829337Z",
            "url": "https://files.pythonhosted.org/packages/fa/3f/ac70377b4d33f50d09f284341e885b49d3d1ccf26343e39f030041d348a1/z3c.pagelet-3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "55ac0720cf56d9dd38870ac614879ad657c69c9b042dde49a7f3d08dfd3356f0",
                "md5": "ab5e15d917c823a1ed952430d44ca725",
                "sha256": "13010786838bce062f1b11587a3bdcc7a8e945d95439b1671f2a4eb7d55ad925"
            },
            "downloads": -1,
            "filename": "z3c.pagelet-3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "ab5e15d917c823a1ed952430d44ca725",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 29331,
            "upload_time": "2024-10-21T07:01:49",
            "upload_time_iso_8601": "2024-10-21T07:01:49.630410Z",
            "url": "https://files.pythonhosted.org/packages/55/ac/0720cf56d9dd38870ac614879ad657c69c9b042dde49a7f3d08dfd3356f0/z3c.pagelet-3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-21 07:01:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zopefoundation",
    "github_project": "z3c.pagelet",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "z3c.pagelet"
}
        
Elapsed time: 0.35281s