Skip to content

Latest commit

 

History

History
69 lines (45 loc) · 2.84 KB

grok.rst

File metadata and controls

69 lines (45 loc) · 2.84 KB

Grok

Grok is an alternative declaration language for declaring your components. It is compatible with the Zope Component Architecture, it used just an alternative syntax.

Instead of writing separate zcml files, you annotate your code and you create content conforming to specific file names so that they are automatically found. There has been discussions whether grok should be used in the plone core. The plone community decided against it, because it increases the technology stack without adding functionality.

Some people are even against using it in Add Ons, because there would not be just one way to declare components, but two. Then there is onle last disadvantage, grok components cannot be overridden by z3c.jbot. I would not be surprised if this could be fixed though.

After all these negative things let us tell you why we still like it: We like to write as few lines of code and configuration as possible.

So, we will write our browser view as a grok view. From the component architecture side, nothing changes. We still need to write a multi adapter. All the details like which template to use or for which browser layer the view shall be used is declared with a single line annotation or deduced from file names.

.. seealso::

    http://docs.plone.org/develop/addons/five-grok/index.html

Grok is not part of plone. We have to add it as a dependency to our egg.

Open setup.py, and add five.grok to the list off addons in install_requires:

...
    zip_safe=False,
    install_requires=[
        'setuptools',
        'five.grok',
    ],

You need to run buildout now.

Grok nearly magicaly does find all its annotations. Since its not complete magic, you have to tell grok where to look for grok code. This requires a single line of zcml, that line ensures that your complete package is grokked.

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:five="http://namespaces.zope.org/five"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
    xmlns:grok="http://namespaces.zope.org/grok"
    xmlns:plone="http://namespaces.plone.org/plone"
    i18n_domain="ploneconf.site">

    <includeDependencies package="." />

    <grok:grok package="." />
    ....

This new grok statement takes care of finding everything grok related.

Now we can add a grok view in a new file views.py:

from five import grok
from plone.directives import dexterity
from zope.interface import Interface


class TalkView(dexterity.DisplayForm):
    grok.require("zope2.View")
    grok.context(Interface)

By convention the template must be in a subdirectory called views_templates and it must be named talkview.pt