grokcore.chameleon


Namegrokcore.chameleon JSON
Version 4.0 PyPI version JSON
download
home_pagehttps://pypi.org/project/grokcore.chameleon/
SummaryChameleon page template support for Grok
upload_time2023-02-09 11:06:12
maintainer
docs_urlNone
authorUli Fouquet
requires_python>=3.7
licenseZPL
keywords grok chameleon template
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ==================
grokcore.chameleon
==================

`grokcore.chameleon` makes it possible to use Chameleon page templates in Grok.
For more information on Grok and Chameleon page templates see:

- http://pagetemplates.org/
- http://pagetemplates.org/docs/latest/
- http://pypi.python.org/pypi/Chameleon
- http://grok.zope.org/

.. contents::

Installation
============

Note that future versions of grok will depend itself on grokcore.chameleon
and configure it. In other words, chameleon-based templates will be available
by default from that version on!

To use Chameleon page templates with Grok all you need is to install
grokcore.chameleon as an egg and include its ZCML. The best place to do
this is to make `grokcore.chameleon` a dependency of your application by
adding it to your ``install_requires`` list in ``setup.cfg``. If you
used grokproject to create your application ``setup.py`` is located in the
project root. It should look something like this::

   install_requires=['setuptools',
                     'grokcore.chameleon',
                     # Add extra requirements here
                     ],

Note that if you use the ``allow-picked-versions = false`` directive in your
project's ``buildout.cfg``, you will have to add version number specifications
for several packages to your ``[versions]`` section.

Then include ``grokcore.chameleon`` in your ``configure.zcml``. If you used
grokproject to create your application it's at
``src/<projectname>/configure.zcml``. Add the include line after the include
line for grok, but before the grokking of the current package. It should look
something like this::

      <include package="grok" />
      <include package="grokcore.chameleon" />
      <grok:grok package="." />

If you use ``autoInclude`` in your ``configure.zcml``, you should not
have to do this latter step.

Then run ``bin/buildout`` again. You should now see buildout saying
something like (where version numbers can vary)::

   Getting distribution for 'grokcore.chameleon'.
   Got grokcore.chameleon 0.5.

That's all. You can now start using Chameleon page templates in your
Grok application.

Usage
=====

``grokcore.chameleon`` supports the Grok standard of placing templates in a
templates directory, for example ``app_templates``, so you can use Chameleon
page templates by simply placing the Chameleon page templates in the templates
directory, just as you would with regular ZPT templates.

Although chameleon templates themselves do not have a standard for the file
extensions for templates, Grok needs to have an association between an filename
extension and a template language implementation so it knows which
implementation to use.

`grokcore.chameleon` declares to use the extension ``*.cpt`` (``Chameleon page
template``) for Chameleon page templates.

You can also use Chameleon page templates inline. The syntax for this
is::

   from grokcore.chameleon.components import ChameleonPageTemplate
   index = ChameleonPageTemplate('<html>the html code</html>')

Or if you use files::

   from grokcore.chameleon.components import ChameleonPageTemplateFile
   index = ChameleonPageTemplateFile(filename='thefilename.html')


====================
Detailed Description
====================

Grok-support for using chameleon driven templates.

With `grokcore.chameleon` you can use templates parsed and rendered by
`Chameleon`_ using the Zope Page Template templating language.

Chameleon Zope page templates
=============================

Chameleon provides support for Zope page templates which can be used
from grok writing templates with the ``.cpt`` (=Chameleon Page
Template) filename extension.

Chameleon page templates differ from standard Zope page templates in a
few aspects, most notably:

* Expressions are parsed in ``Python-mode`` by default. This means,
  instead of ``tal:content="view/value"`` you must use
  ``tal:content="view.value"``. Every occurence of TAL-expressions
  starting with ``python:`` now can be shortened by skipping this
  marker.

* Also Genshi-like variable substitutions are supported. For example
  you can write ``${myvar}`` instead of ``tal:content="myvar"``.

Beside this, most rules for regular Zope page templates apply also to
chameleon page templates.

See the `Chameleon`_ page for more information.

.. _Chameleon: http://chameleon.repoze.org/docs/latest/zpt.html

Prerequisites
=============

Before we can see the templates in action, we care for correct
registration and set some used variables:

    >>> import os
    >>> testdir = os.path.join(os.path.dirname(__file__), 'tests')
    >>> cpt_fixture = os.path.join(testdir, 'cpt_fixture')
    >>> template_dir = os.path.join(cpt_fixture, 'app_templates')

We register everything. Before we can grok our fixture, we have to
grok the `grokcore.chameleon` package. This way the new template types
are registered with the framework:

    >>> import grokcore.view
    >>> grokcore.view.testing.grok('grokcore.chameleon')
    >>> grokcore.view.testing.grok('grokcore.chameleon.tests.cpt_fixture')

We create a mammoth, which should provide us a bunch of chameleon page
template driven views and put it in the database to setup location
info::

    >>> from grokcore.chameleon.tests.cpt_fixture.app import Mammoth
    >>> manfred = Mammoth()
    >>> getRootFolder()['manfred'] = manfred

Furthermore we prepare for getting the different views on manfred:

    >>> from zope.publisher.browser import TestRequest
    >>> from zope.component import getMultiAdapter
    >>> request = TestRequest()

Simple templates
================

We prepared a plain cavepainting view. The template looks like this:

    >>> cavepainting_cpt = os.path.join(template_dir, 'cavepainting.cpt')
    >>> with open(cavepainting_cpt, 'r') as f:
    ...     print(f.read())
    <html>
      <body>
        A cave painting.
      </body>
    </html>

The rendered view looks like this:

    >>> view = getMultiAdapter((manfred, request),
    ...                         name='cavepainting')
    >>> print(view())
    <html>
      <body>
        A cave painting.
      </body>
    </html>

Substituting variables
======================

A template can access variables like ``view``, ``context``, ``static``
and its methods and attributes. The ``food`` view does exactly
this. The template looks like this:

    >>> food_cpt = os.path.join(template_dir, 'food.cpt')
    >>> with open(food_cpt, 'r') as f:
    ...     print(f.read())
    <html>
    <body>
    <span tal:define="foo 'a FOO'">
    ${view.me_do()}
    <span tal:replace="structure view.me_do()" />
    CSS-URL: ${path:static/test.css}
    My context is: ${view.url(context)}
    ${foo}
    <span tal:replace="foo" />
    </span>
    </body>
    </html>

The rendered view looks like this:

    >>> view = getMultiAdapter((manfred, request), name='food')
    >>> print(view())
    <html>
    <body>
    <span>
    &lt;ME GROK EAT MAMMOTH!&gt;
    <ME GROK EAT MAMMOTH!>
    CSS-URL: dummy:/test.css
    My context is: http://127.0.0.1/manfred
    a FOO
    a FOO
    </span>
    </body>
    </html>

As we can see, there is a difference between Genshi-like substitution
and TAL-like substitution: while both expressions::

  ${view.me_do()}

and::

  <span tal:replace="view.me_do()" />

actually render the same string ``<ME GROK EAT MAMMOTH!>``, the former
does this straight and plain, while the latter performs additionally
HTML-encoding of the string. Therefore the output of both expressions
differ. It's::

  <ME GROK EAT MAMMOTH!>

for the former expression and::

  &lt;ME GROK EAT MAMMOTH!&gt;

for the latter.


Supported variables
===================

Each template provides at least the following vars:

* ``template``
    the template instance

* ``view``
    the associated view

* ``context``
    the context of the view

* ``request``
    the current request

as we can see, when we look at the ``vars.cpt`` from our fixture:

    >>> cpt_file = os.path.join(template_dir, 'vars.cpt')
    >>> with open(cpt_file, 'r') as f:
    ...     print(f.read())
    <html>
    <body>
    This template knows about the following vars:
    <BLANKLINE>
      template (the template instance):
       ${template}
    <BLANKLINE>
      view (the associated view):
       ${view}
    <BLANKLINE>
      context (the context of the view):
       ${context}
    <BLANKLINE>
      request (the current request):
       ${request}
    </body>
    </html>

and render it:

    >>> view = getMultiAdapter((manfred, request), name='vars')
    >>> print(view())
    <html>
    <body>
    This template knows about the following vars:
    <BLANKLINE>
      template (the template instance):
       &lt;PageTemplateFile ...vars.cpt&gt;
    <BLANKLINE>
      view (the associated view):
       &lt;grokcore.chameleon.tests.cpt_fixture.app.Vars object at 0x...&gt;
    <BLANKLINE>
      context (the context of the view):
       &lt;grokcore.chameleon.tests.cpt_fixture.app.Mammoth object at 0x...&gt;
    <BLANKLINE>
      request (the current request):
       CONTENT_LENGTH:	0
    GATEWAY_INTERFACE:	TestFooInterface/1.0
    HTTP_HOST:	127.0.0.1
    SERVER_URL:	http://127.0.0.1
    </body>
    </html>

Custom template namespace names are supported:

    >>> view = getMultiAdapter((manfred, request), name='namespace')
    >>> print(view())
    <html>
    <body>
    This template knows about the following custom namespace name:
    <BLANKLINE>
      myname:
       Henk
    <BLANKLINE>
    </body>
    </html>

Inline Templates
================

We can also define inline templates. In our ``app.py`` we defined an
inline template like this::

  from grokcore.chameleon import components

  ...

  class Inline(grokcore.view.View):
    sometext = 'Some Text'

  inline = components.ChameleonPageTemplate(
      "<html><body>ME GROK HAS INLINES! ${view.sometext}</body></html>")

If we render this view we get:

    >>> view = getMultiAdapter((manfred, request), name='inline')
    >>> print(view())
    <html><body>ME GROK HAS INLINES! Some Text</body></html>

TAL expressions
===============

Starting with ``grokcore.chameleon`` 0.5 we deploy the all-in-one
`Chameleon`_ package.

What TAL/TALES expressions in templates are supported depends mainly
from the installed version of `Chameleon`, while we support some
additional, Zope-related TALES expressions.

A list of all supported expressions and statements can be found at the
`chameleon.zpt documentation
<http://chameleon.repoze.org/docs/latest/zpt.html>`_. The additional
TALES expressions provided by ``grokcore.chameleon`` are:

* ``exists``
     Tell whether a name exists in the templates' namespace.

* ``not``
     Evaluate the expression to a boolean value and invert it.

* ``path``
     Handle the expression as a path and not as a Python expression.

* ``provider``
     Support for viewlet providers.

.. note:: Starting with ``grokcore.chameleon`` 0.5 support for the
          Python expression ``exists()`` has been dropped. The TALES
          expression ``exists: path/to/something`` is still available.

In our ``app.py`` we defined a special view for showing some special
expressions. This also includes a viewlet::

   import grok
   from grokcore.chameleon import components

   class Mammoth(grok.Application, grok.Container):
       pass

   ...

   class Expressions(grok.View):
       pass

   class MainArea(grok.ViewletManager):
       grok.name('main')

   class MainContent(grok.Viewlet):
       grok.view(Expressions)
       grok.viewletmanager(MainArea)
       def render(self):
           return 'Hello from viewlet'

Now we can make use of the TALES expressions ``not:``, ``path:``,
``exists:`` and ``provider:`` in the ``expressions.cpt`` template of
our fixture:

    >>> cpt_file = os.path.join(template_dir, 'expressions.cpt')
    >>> with open(cpt_file, 'r') as f:
    ...     print(f.read())
    <html>
    <body>
      <div tal:define="food 'Yummy Dinoburger'"
           tal:omit-tag="">
        <!-- We support `exists` -->
        <div tal:condition="exists: food">
          ${food}
        </div>
    <BLANKLINE>
        <!-- We support `not` -->
        <div tal:content="not: food" />
        <div tal:content="not('food')" />
        <div tal:content="not: 1 in [2,3]" />
        <div tal:content="not: not: food" />
    <BLANKLINE>
        <!-- We support `path` -->
        <div tal:content="path: food/upper" />
    <BLANKLINE>
        <!-- We support `provider` -->
        <tal:main content="structure provider:main" />
    <BLANKLINE>
      </div>
    </body>
    </html>

and render it:

    >>> view = getMultiAdapter((manfred, request), name='expressions')
    >>> print(view())
    <html>
    <body>
    <BLANKLINE>
        <!-- We support `exists` -->
        <div>
          Yummy Dinoburger
        </div>
    <BLANKLINE>
        <!-- We support `not` -->
        <div>False</div>
        <div>False</div>
        <div>True</div>
        <div>True</div>
    <BLANKLINE>
        <!-- We support `path` -->
        <div>YUMMY DINOBURGER</div>
    <BLANKLINE>
        <!-- We support `provider` -->
        Hello from viewlet
    <BLANKLINE>
    <BLANKLINE>
    </body>
    </html>

Translation
===========

    >>> # Monkeypatch zope.i18n.negotiate
    >>> import zope.i18n
    >>> import zope.i18n.config
    >>> print(getMultiAdapter((manfred, request), name='menu')())
    <html>
    <body>
      <h1>Menu</h1>
      <ol>
        <li>Deepfried breaded veal cutlets</li>
      </ol>
    </body>
    </html>

    >>> # What's for food today in Germany?
    >>> # We need to monkey patch the language settings for this test.
    >>> old_1, old_2 = zope.i18n.negotiate, zope.i18n.config.ALLOWED_LANGUAGES
    >>> zope.i18n.negotiate = lambda context: 'de'
    >>> zope.i18n.config.ALLOWED_LANGUAGES = ['de']
    >>> print(getMultiAdapter((manfred, request), name='menu')())
    <html>
    <body>
      <h1>Menu</h1>
      <ol>
        <li>Schnitzel</li>
      </ol>
    </body>
    </html>

    >>> # Restore the monkey patch.
    >>> zope.i18n.negotiate, zope.i18n.config.ALLOWED_LANGUAGES = old_1, old_2

Macros
======

With ``grokcore.chameleon`` we can also use macros, although it is a bit
different from regular Zope page templates.

We can define macros like this:

    >>> cpt_file = os.path.join(template_dir, 'macromaster.cpt')
    >>> with open(cpt_file, 'r') as f:
    ...     print(f.read())
    <p xmlns:metal="http://xml.zope.org/namespaces/metal"
       metal:define-macro="hello">
      Hello from <b metal:define-slot="name">macro master</b>
    </p>

The defined macro ``hello`` can be rendered in another Chameleon
template with the METAL attribute ``use-macro``.

To refer to a local macro, i.e. a macros defined in the same template,
you can use something like::

  <div metal:use-macro="template.macros['<macro-name>']">
    Replaced by macro
  </div>

where ``<macro-name>`` must be an existing macro name.

To refer to macros in external templates, you must use the ``path:`` expression
like this::

  <div metal:use-macro="path:
    context/@@<viewname>/template/macros/<macro-name>">
     Replaced by external macro
  </div>

where ``<viewname>`` refers to an existing view on ``context`` and ``macro-
name`` again refers to an existing macro in the specified template.

Note, that this is different from how you refer to macros in standard Zope page
templates. The short notation ``view/macros/<macro-name>`` works only with
regular Zope page templates.

The following template makes use of both methods:

    >>> cpt_file = os.path.join(template_dir, 'macrouser.cpt')
    >>> with open(cpt_file, 'r') as f:
    ...     print(f.read())
    <html xmlns:metal="http://xml.zope.org/namespaces/metal">
    <body>
      <p metal:define-macro="hello">
        Hi there from macro user!
      </p>
      <div metal:use-macro="template.macros['hello']">
        Fill this
      </div>
    <BLANKLINE>
      <div metal:use-macro="path: context/@@macromaster/template/macros/hello">
        <b metal:fill-slot="name">user slot</b>
        Fill this too
      </div>
    </body>
    </html>

When rendered also the slot defined in the master template is filled by macro
user content:

    >>> cpt_file = os.path.join(template_dir, 'macrouser.cpt')
    >>> view = getMultiAdapter((manfred, request), name='macrouser')
    >>> print(view())
    <html>
    <body>
      <p>
        Hi there from macro user!
      </p>
      <p>
        Hi there from macro user!
      </p>
    <BLANKLINE>
    <BLANKLINE>
      <p>
      Hello from <b>user slot</b>
    <BLANKLINE>
    </p>
    </body>
    </html>


Clean up:

    >>> del getRootFolder()['manfred']

Differences from regular Zope page templates
============================================

* Macros are referenced differently. See appropriate section above.

* Expressions are parsed in ``Python-mode`` by default. This means, instead
  of ``tal:content="view/value"`` you must use ``tal:content="view.value"``.
  Every occurence of TAL-expressions starting with ``python:`` now can be
  shortened by skipping this marker.


CHANGES
*******

4.0 (2023-02-09)
================

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

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


3.0.1 (2018-01-12)
==================

- Rearrange tests such that Travis CI can pick up all functional tests too.

3.0.0 (2018-01-11)
==================

- Python 3 compatibility.

1.0.4 (2014-07-29)
==================

- Improve the performances of the translate mechanism with Chameleon
  2.10 or more recent.

1.0.3 (2012-10-12)
==================

- Fix broken translations when using Chameleon 2.9 or more recent.

1.0.2 (2012-05-07)
==================

- With not using the z3c.pt PageTemplateFile baseclass, the behaviour of
  finding the template file relative to the module was lost. This has been
  fixed.

1.0.1 (2012-05-03)
==================

- Make sure the minimal version requirements are defined.

1.0 (2012-05-01)
================

- The ``target_language`` mangling was lost in version 1.0rc4.
  Copied from z3c.pt.

1.0rc4 (2012-01-03)
===================

- Update to newes Chameleon 2.7.1
- Using some Components/Expressions directly from Chameleon instead of z3c.pt

1.0rc3 (2011-07-14)
===================

- Rename megrok.chameleon into grokcore.chameleon to make it an official part
  of Grok.

Earlier versions
================

- Earlier versions of grokcore.chameleon came by the name megrok.chameleon.

            

Raw data

            {
    "_id": null,
    "home_page": "https://pypi.org/project/grokcore.chameleon/",
    "name": "grokcore.chameleon",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "grok chameleon template",
    "author": "Uli Fouquet",
    "author_email": "zope-dev@zope.dev",
    "download_url": "https://files.pythonhosted.org/packages/aa/ea/688acefde2fbb8a0c920db137cacce5adae1bcd9279f82a55f9082d9f534/grokcore.chameleon-4.0.tar.gz",
    "platform": null,
    "description": "==================\ngrokcore.chameleon\n==================\n\n`grokcore.chameleon` makes it possible to use Chameleon page templates in Grok.\nFor more information on Grok and Chameleon page templates see:\n\n- http://pagetemplates.org/\n- http://pagetemplates.org/docs/latest/\n- http://pypi.python.org/pypi/Chameleon\n- http://grok.zope.org/\n\n.. contents::\n\nInstallation\n============\n\nNote that future versions of grok will depend itself on grokcore.chameleon\nand configure it. In other words, chameleon-based templates will be available\nby default from that version on!\n\nTo use Chameleon page templates with Grok all you need is to install\ngrokcore.chameleon as an egg and include its ZCML. The best place to do\nthis is to make `grokcore.chameleon` a dependency of your application by\nadding it to your ``install_requires`` list in ``setup.cfg``. If you\nused grokproject to create your application ``setup.py`` is located in the\nproject root. It should look something like this::\n\n   install_requires=['setuptools',\n                     'grokcore.chameleon',\n                     # Add extra requirements here\n                     ],\n\nNote that if you use the ``allow-picked-versions = false`` directive in your\nproject's ``buildout.cfg``, you will have to add version number specifications\nfor several packages to your ``[versions]`` section.\n\nThen include ``grokcore.chameleon`` in your ``configure.zcml``. If you used\ngrokproject to create your application it's at\n``src/<projectname>/configure.zcml``. Add the include line after the include\nline for grok, but before the grokking of the current package. It should look\nsomething like this::\n\n      <include package=\"grok\" />\n      <include package=\"grokcore.chameleon\" />\n      <grok:grok package=\".\" />\n\nIf you use ``autoInclude`` in your ``configure.zcml``, you should not\nhave to do this latter step.\n\nThen run ``bin/buildout`` again. You should now see buildout saying\nsomething like (where version numbers can vary)::\n\n   Getting distribution for 'grokcore.chameleon'.\n   Got grokcore.chameleon 0.5.\n\nThat's all. You can now start using Chameleon page templates in your\nGrok application.\n\nUsage\n=====\n\n``grokcore.chameleon`` supports the Grok standard of placing templates in a\ntemplates directory, for example ``app_templates``, so you can use Chameleon\npage templates by simply placing the Chameleon page templates in the templates\ndirectory, just as you would with regular ZPT templates.\n\nAlthough chameleon templates themselves do not have a standard for the file\nextensions for templates, Grok needs to have an association between an filename\nextension and a template language implementation so it knows which\nimplementation to use.\n\n`grokcore.chameleon` declares to use the extension ``*.cpt`` (``Chameleon page\ntemplate``) for Chameleon page templates.\n\nYou can also use Chameleon page templates inline. The syntax for this\nis::\n\n   from grokcore.chameleon.components import ChameleonPageTemplate\n   index = ChameleonPageTemplate('<html>the html code</html>')\n\nOr if you use files::\n\n   from grokcore.chameleon.components import ChameleonPageTemplateFile\n   index = ChameleonPageTemplateFile(filename='thefilename.html')\n\n\n====================\nDetailed Description\n====================\n\nGrok-support for using chameleon driven templates.\n\nWith `grokcore.chameleon` you can use templates parsed and rendered by\n`Chameleon`_ using the Zope Page Template templating language.\n\nChameleon Zope page templates\n=============================\n\nChameleon provides support for Zope page templates which can be used\nfrom grok writing templates with the ``.cpt`` (=Chameleon Page\nTemplate) filename extension.\n\nChameleon page templates differ from standard Zope page templates in a\nfew aspects, most notably:\n\n* Expressions are parsed in ``Python-mode`` by default. This means,\n  instead of ``tal:content=\"view/value\"`` you must use\n  ``tal:content=\"view.value\"``. Every occurence of TAL-expressions\n  starting with ``python:`` now can be shortened by skipping this\n  marker.\n\n* Also Genshi-like variable substitutions are supported. For example\n  you can write ``${myvar}`` instead of ``tal:content=\"myvar\"``.\n\nBeside this, most rules for regular Zope page templates apply also to\nchameleon page templates.\n\nSee the `Chameleon`_ page for more information.\n\n.. _Chameleon: http://chameleon.repoze.org/docs/latest/zpt.html\n\nPrerequisites\n=============\n\nBefore we can see the templates in action, we care for correct\nregistration and set some used variables:\n\n    >>> import os\n    >>> testdir = os.path.join(os.path.dirname(__file__), 'tests')\n    >>> cpt_fixture = os.path.join(testdir, 'cpt_fixture')\n    >>> template_dir = os.path.join(cpt_fixture, 'app_templates')\n\nWe register everything. Before we can grok our fixture, we have to\ngrok the `grokcore.chameleon` package. This way the new template types\nare registered with the framework:\n\n    >>> import grokcore.view\n    >>> grokcore.view.testing.grok('grokcore.chameleon')\n    >>> grokcore.view.testing.grok('grokcore.chameleon.tests.cpt_fixture')\n\nWe create a mammoth, which should provide us a bunch of chameleon page\ntemplate driven views and put it in the database to setup location\ninfo::\n\n    >>> from grokcore.chameleon.tests.cpt_fixture.app import Mammoth\n    >>> manfred = Mammoth()\n    >>> getRootFolder()['manfred'] = manfred\n\nFurthermore we prepare for getting the different views on manfred:\n\n    >>> from zope.publisher.browser import TestRequest\n    >>> from zope.component import getMultiAdapter\n    >>> request = TestRequest()\n\nSimple templates\n================\n\nWe prepared a plain cavepainting view. The template looks like this:\n\n    >>> cavepainting_cpt = os.path.join(template_dir, 'cavepainting.cpt')\n    >>> with open(cavepainting_cpt, 'r') as f:\n    ...     print(f.read())\n    <html>\n      <body>\n        A cave painting.\n      </body>\n    </html>\n\nThe rendered view looks like this:\n\n    >>> view = getMultiAdapter((manfred, request),\n    ...                         name='cavepainting')\n    >>> print(view())\n    <html>\n      <body>\n        A cave painting.\n      </body>\n    </html>\n\nSubstituting variables\n======================\n\nA template can access variables like ``view``, ``context``, ``static``\nand its methods and attributes. The ``food`` view does exactly\nthis. The template looks like this:\n\n    >>> food_cpt = os.path.join(template_dir, 'food.cpt')\n    >>> with open(food_cpt, 'r') as f:\n    ...     print(f.read())\n    <html>\n    <body>\n    <span tal:define=\"foo 'a FOO'\">\n    ${view.me_do()}\n    <span tal:replace=\"structure view.me_do()\" />\n    CSS-URL: ${path:static/test.css}\n    My context is: ${view.url(context)}\n    ${foo}\n    <span tal:replace=\"foo\" />\n    </span>\n    </body>\n    </html>\n\nThe rendered view looks like this:\n\n    >>> view = getMultiAdapter((manfred, request), name='food')\n    >>> print(view())\n    <html>\n    <body>\n    <span>\n    &lt;ME GROK EAT MAMMOTH!&gt;\n    <ME GROK EAT MAMMOTH!>\n    CSS-URL: dummy:/test.css\n    My context is: http://127.0.0.1/manfred\n    a FOO\n    a FOO\n    </span>\n    </body>\n    </html>\n\nAs we can see, there is a difference between Genshi-like substitution\nand TAL-like substitution: while both expressions::\n\n  ${view.me_do()}\n\nand::\n\n  <span tal:replace=\"view.me_do()\" />\n\nactually render the same string ``<ME GROK EAT MAMMOTH!>``, the former\ndoes this straight and plain, while the latter performs additionally\nHTML-encoding of the string. Therefore the output of both expressions\ndiffer. It's::\n\n  <ME GROK EAT MAMMOTH!>\n\nfor the former expression and::\n\n  &lt;ME GROK EAT MAMMOTH!&gt;\n\nfor the latter.\n\n\nSupported variables\n===================\n\nEach template provides at least the following vars:\n\n* ``template``\n    the template instance\n\n* ``view``\n    the associated view\n\n* ``context``\n    the context of the view\n\n* ``request``\n    the current request\n\nas we can see, when we look at the ``vars.cpt`` from our fixture:\n\n    >>> cpt_file = os.path.join(template_dir, 'vars.cpt')\n    >>> with open(cpt_file, 'r') as f:\n    ...     print(f.read())\n    <html>\n    <body>\n    This template knows about the following vars:\n    <BLANKLINE>\n      template (the template instance):\n       ${template}\n    <BLANKLINE>\n      view (the associated view):\n       ${view}\n    <BLANKLINE>\n      context (the context of the view):\n       ${context}\n    <BLANKLINE>\n      request (the current request):\n       ${request}\n    </body>\n    </html>\n\nand render it:\n\n    >>> view = getMultiAdapter((manfred, request), name='vars')\n    >>> print(view())\n    <html>\n    <body>\n    This template knows about the following vars:\n    <BLANKLINE>\n      template (the template instance):\n       &lt;PageTemplateFile ...vars.cpt&gt;\n    <BLANKLINE>\n      view (the associated view):\n       &lt;grokcore.chameleon.tests.cpt_fixture.app.Vars object at 0x...&gt;\n    <BLANKLINE>\n      context (the context of the view):\n       &lt;grokcore.chameleon.tests.cpt_fixture.app.Mammoth object at 0x...&gt;\n    <BLANKLINE>\n      request (the current request):\n       CONTENT_LENGTH:\t0\n    GATEWAY_INTERFACE:\tTestFooInterface/1.0\n    HTTP_HOST:\t127.0.0.1\n    SERVER_URL:\thttp://127.0.0.1\n    </body>\n    </html>\n\nCustom template namespace names are supported:\n\n    >>> view = getMultiAdapter((manfred, request), name='namespace')\n    >>> print(view())\n    <html>\n    <body>\n    This template knows about the following custom namespace name:\n    <BLANKLINE>\n      myname:\n       Henk\n    <BLANKLINE>\n    </body>\n    </html>\n\nInline Templates\n================\n\nWe can also define inline templates. In our ``app.py`` we defined an\ninline template like this::\n\n  from grokcore.chameleon import components\n\n  ...\n\n  class Inline(grokcore.view.View):\n    sometext = 'Some Text'\n\n  inline = components.ChameleonPageTemplate(\n      \"<html><body>ME GROK HAS INLINES! ${view.sometext}</body></html>\")\n\nIf we render this view we get:\n\n    >>> view = getMultiAdapter((manfred, request), name='inline')\n    >>> print(view())\n    <html><body>ME GROK HAS INLINES! Some Text</body></html>\n\nTAL expressions\n===============\n\nStarting with ``grokcore.chameleon`` 0.5 we deploy the all-in-one\n`Chameleon`_ package.\n\nWhat TAL/TALES expressions in templates are supported depends mainly\nfrom the installed version of `Chameleon`, while we support some\nadditional, Zope-related TALES expressions.\n\nA list of all supported expressions and statements can be found at the\n`chameleon.zpt documentation\n<http://chameleon.repoze.org/docs/latest/zpt.html>`_. The additional\nTALES expressions provided by ``grokcore.chameleon`` are:\n\n* ``exists``\n     Tell whether a name exists in the templates' namespace.\n\n* ``not``\n     Evaluate the expression to a boolean value and invert it.\n\n* ``path``\n     Handle the expression as a path and not as a Python expression.\n\n* ``provider``\n     Support for viewlet providers.\n\n.. note:: Starting with ``grokcore.chameleon`` 0.5 support for the\n          Python expression ``exists()`` has been dropped. The TALES\n          expression ``exists: path/to/something`` is still available.\n\nIn our ``app.py`` we defined a special view for showing some special\nexpressions. This also includes a viewlet::\n\n   import grok\n   from grokcore.chameleon import components\n\n   class Mammoth(grok.Application, grok.Container):\n       pass\n\n   ...\n\n   class Expressions(grok.View):\n       pass\n\n   class MainArea(grok.ViewletManager):\n       grok.name('main')\n\n   class MainContent(grok.Viewlet):\n       grok.view(Expressions)\n       grok.viewletmanager(MainArea)\n       def render(self):\n           return 'Hello from viewlet'\n\nNow we can make use of the TALES expressions ``not:``, ``path:``,\n``exists:`` and ``provider:`` in the ``expressions.cpt`` template of\nour fixture:\n\n    >>> cpt_file = os.path.join(template_dir, 'expressions.cpt')\n    >>> with open(cpt_file, 'r') as f:\n    ...     print(f.read())\n    <html>\n    <body>\n      <div tal:define=\"food 'Yummy Dinoburger'\"\n           tal:omit-tag=\"\">\n        <!-- We support `exists` -->\n        <div tal:condition=\"exists: food\">\n          ${food}\n        </div>\n    <BLANKLINE>\n        <!-- We support `not` -->\n        <div tal:content=\"not: food\" />\n        <div tal:content=\"not('food')\" />\n        <div tal:content=\"not: 1 in [2,3]\" />\n        <div tal:content=\"not: not: food\" />\n    <BLANKLINE>\n        <!-- We support `path` -->\n        <div tal:content=\"path: food/upper\" />\n    <BLANKLINE>\n        <!-- We support `provider` -->\n        <tal:main content=\"structure provider:main\" />\n    <BLANKLINE>\n      </div>\n    </body>\n    </html>\n\nand render it:\n\n    >>> view = getMultiAdapter((manfred, request), name='expressions')\n    >>> print(view())\n    <html>\n    <body>\n    <BLANKLINE>\n        <!-- We support `exists` -->\n        <div>\n          Yummy Dinoburger\n        </div>\n    <BLANKLINE>\n        <!-- We support `not` -->\n        <div>False</div>\n        <div>False</div>\n        <div>True</div>\n        <div>True</div>\n    <BLANKLINE>\n        <!-- We support `path` -->\n        <div>YUMMY DINOBURGER</div>\n    <BLANKLINE>\n        <!-- We support `provider` -->\n        Hello from viewlet\n    <BLANKLINE>\n    <BLANKLINE>\n    </body>\n    </html>\n\nTranslation\n===========\n\n    >>> # Monkeypatch zope.i18n.negotiate\n    >>> import zope.i18n\n    >>> import zope.i18n.config\n    >>> print(getMultiAdapter((manfred, request), name='menu')())\n    <html>\n    <body>\n      <h1>Menu</h1>\n      <ol>\n        <li>Deepfried breaded veal cutlets</li>\n      </ol>\n    </body>\n    </html>\n\n    >>> # What's for food today in Germany?\n    >>> # We need to monkey patch the language settings for this test.\n    >>> old_1, old_2 = zope.i18n.negotiate, zope.i18n.config.ALLOWED_LANGUAGES\n    >>> zope.i18n.negotiate = lambda context: 'de'\n    >>> zope.i18n.config.ALLOWED_LANGUAGES = ['de']\n    >>> print(getMultiAdapter((manfred, request), name='menu')())\n    <html>\n    <body>\n      <h1>Menu</h1>\n      <ol>\n        <li>Schnitzel</li>\n      </ol>\n    </body>\n    </html>\n\n    >>> # Restore the monkey patch.\n    >>> zope.i18n.negotiate, zope.i18n.config.ALLOWED_LANGUAGES = old_1, old_2\n\nMacros\n======\n\nWith ``grokcore.chameleon`` we can also use macros, although it is a bit\ndifferent from regular Zope page templates.\n\nWe can define macros like this:\n\n    >>> cpt_file = os.path.join(template_dir, 'macromaster.cpt')\n    >>> with open(cpt_file, 'r') as f:\n    ...     print(f.read())\n    <p xmlns:metal=\"http://xml.zope.org/namespaces/metal\"\n       metal:define-macro=\"hello\">\n      Hello from <b metal:define-slot=\"name\">macro master</b>\n    </p>\n\nThe defined macro ``hello`` can be rendered in another Chameleon\ntemplate with the METAL attribute ``use-macro``.\n\nTo refer to a local macro, i.e. a macros defined in the same template,\nyou can use something like::\n\n  <div metal:use-macro=\"template.macros['<macro-name>']\">\n    Replaced by macro\n  </div>\n\nwhere ``<macro-name>`` must be an existing macro name.\n\nTo refer to macros in external templates, you must use the ``path:`` expression\nlike this::\n\n  <div metal:use-macro=\"path:\n    context/@@<viewname>/template/macros/<macro-name>\">\n     Replaced by external macro\n  </div>\n\nwhere ``<viewname>`` refers to an existing view on ``context`` and ``macro-\nname`` again refers to an existing macro in the specified template.\n\nNote, that this is different from how you refer to macros in standard Zope page\ntemplates. The short notation ``view/macros/<macro-name>`` works only with\nregular Zope page templates.\n\nThe following template makes use of both methods:\n\n    >>> cpt_file = os.path.join(template_dir, 'macrouser.cpt')\n    >>> with open(cpt_file, 'r') as f:\n    ...     print(f.read())\n    <html xmlns:metal=\"http://xml.zope.org/namespaces/metal\">\n    <body>\n      <p metal:define-macro=\"hello\">\n        Hi there from macro user!\n      </p>\n      <div metal:use-macro=\"template.macros['hello']\">\n        Fill this\n      </div>\n    <BLANKLINE>\n      <div metal:use-macro=\"path: context/@@macromaster/template/macros/hello\">\n        <b metal:fill-slot=\"name\">user slot</b>\n        Fill this too\n      </div>\n    </body>\n    </html>\n\nWhen rendered also the slot defined in the master template is filled by macro\nuser content:\n\n    >>> cpt_file = os.path.join(template_dir, 'macrouser.cpt')\n    >>> view = getMultiAdapter((manfred, request), name='macrouser')\n    >>> print(view())\n    <html>\n    <body>\n      <p>\n        Hi there from macro user!\n      </p>\n      <p>\n        Hi there from macro user!\n      </p>\n    <BLANKLINE>\n    <BLANKLINE>\n      <p>\n      Hello from <b>user slot</b>\n    <BLANKLINE>\n    </p>\n    </body>\n    </html>\n\n\nClean up:\n\n    >>> del getRootFolder()['manfred']\n\nDifferences from regular Zope page templates\n============================================\n\n* Macros are referenced differently. See appropriate section above.\n\n* Expressions are parsed in ``Python-mode`` by default. This means, instead\n  of ``tal:content=\"view/value\"`` you must use ``tal:content=\"view.value\"``.\n  Every occurence of TAL-expressions starting with ``python:`` now can be\n  shortened by skipping this marker.\n\n\nCHANGES\n*******\n\n4.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.7, 3.8, 3.9, 3.10, 3.11.\n\n\n3.0.1 (2018-01-12)\n==================\n\n- Rearrange tests such that Travis CI can pick up all functional tests too.\n\n3.0.0 (2018-01-11)\n==================\n\n- Python 3 compatibility.\n\n1.0.4 (2014-07-29)\n==================\n\n- Improve the performances of the translate mechanism with Chameleon\n  2.10 or more recent.\n\n1.0.3 (2012-10-12)\n==================\n\n- Fix broken translations when using Chameleon 2.9 or more recent.\n\n1.0.2 (2012-05-07)\n==================\n\n- With not using the z3c.pt PageTemplateFile baseclass, the behaviour of\n  finding the template file relative to the module was lost. This has been\n  fixed.\n\n1.0.1 (2012-05-03)\n==================\n\n- Make sure the minimal version requirements are defined.\n\n1.0 (2012-05-01)\n================\n\n- The ``target_language`` mangling was lost in version 1.0rc4.\n  Copied from z3c.pt.\n\n1.0rc4 (2012-01-03)\n===================\n\n- Update to newes Chameleon 2.7.1\n- Using some Components/Expressions directly from Chameleon instead of z3c.pt\n\n1.0rc3 (2011-07-14)\n===================\n\n- Rename megrok.chameleon into grokcore.chameleon to make it an official part\n  of Grok.\n\nEarlier versions\n================\n\n- Earlier versions of grokcore.chameleon came by the name megrok.chameleon.\n",
    "bugtrack_url": null,
    "license": "ZPL",
    "summary": "Chameleon page template support for Grok",
    "version": "4.0",
    "split_keywords": [
        "grok",
        "chameleon",
        "template"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "04d58483cf26e3213624cb4fb2968829fc27c4aeb619d51b3a6f7dbda61d1634",
                "md5": "652555f7a639e9e8c9975f49dbc11613",
                "sha256": "360608bd52a27ef7da172fd89a4f1fad6a08de57487f0fd934329a1f5bd86388"
            },
            "downloads": -1,
            "filename": "grokcore.chameleon-4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "652555f7a639e9e8c9975f49dbc11613",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 24383,
            "upload_time": "2023-02-09T11:06:11",
            "upload_time_iso_8601": "2023-02-09T11:06:11.230108Z",
            "url": "https://files.pythonhosted.org/packages/04/d5/8483cf26e3213624cb4fb2968829fc27c4aeb619d51b3a6f7dbda61d1634/grokcore.chameleon-4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "aaea688acefde2fbb8a0c920db137cacce5adae1bcd9279f82a55f9082d9f534",
                "md5": "bd3e5e932bce7050fc63c4dab3a50516",
                "sha256": "d26f1074f014cd4710c59d177b6bbf8477d3b16c134d545e6faddd8eeb5f7a59"
            },
            "downloads": -1,
            "filename": "grokcore.chameleon-4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "bd3e5e932bce7050fc63c4dab3a50516",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 25106,
            "upload_time": "2023-02-09T11:06:12",
            "upload_time_iso_8601": "2023-02-09T11:06:12.972620Z",
            "url": "https://files.pythonhosted.org/packages/aa/ea/688acefde2fbb8a0c920db137cacce5adae1bcd9279f82a55f9082d9f534/grokcore.chameleon-4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-09 11:06:12",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "grokcore.chameleon"
}
        
Elapsed time: 0.03844s