Joan Perals
Joan Perals

Reputation: 75

jbot not overriding my template

The view I want to override is defined by the class "ActivateForm" in a file called "covalent_member.py" inside the "behaviors" folder of the "ixds.covalent" package. So the place to put a custom template for my view inside this package (ixds.covalent) is

ixds/covalent/behaviors/covalent_member_templates/activateform.pt

(This works)

But then I try to do my customizations in my theme package instead, by creating a file called

ixds.covalent.behaviors.covalent_member_templates.activateform.pt

in my theme's "override" folder, and nothing happens (of course I tried restarting the instance, disabling and re-enabling the theme, reinstalling several products, or even running buildout). Whether the custom template "activateform.pt" still exists in my ixds.covalent package or not, doesn't seem to make any difference (well, actually yes: if I remove it I will get a "No such file or directory" error. The problem however is always the same: the template in the theme's "override" directory is never fetched).

I have 5 other override templates in the same "overrides" folder and all of them are working. I'm using z3c.jbot 0.7.1 (latest available), Plone 4.2.4 (latest stable) and latest available Git version of ixds.covalent from Github.

What can I be doing wrong?

Upvotes: 3

Views: 364

Answers (1)

Dan Jacka
Dan Jacka

Reputation: 1782

I'll assume you're using this ixds.covalent on Github.

There are two different template registering mechanisms at play here.

The ActivateForm class derives from plone.directives.form.Form, which in turn uses the five.grok package. Therefore ActivateForm is "grokked" at startup, meaning that its registration in Zope is done automatically, without a separate entry in another file. Very convenient for the developers of ixds.covalent.

The grok mechanism also allows the developers to create an automatically-registered template for the form. They can create a directory with the name of the module plus '_templates' (covalent_member_templates) and a file matching the class name (activateform.pt).

But in this case, the developers have not chosen to do that. plone.directives.form exists to make form creation easier for developers, e.g. by not requiring a dedicated template. As you have seen, there's nothing to stop you from creating that template in the ixds.covalent package following the standard grok approach.

But of course editing third-party packages in this way is not advisable. You are right to customise the form in your own package. But you cannot use z3c.jbot because there is no existing template to override. You must override the ActivateForm class and use the grok template technique yourself.

So, in your my.theme package make sure you have in interfaces.py:

from zope.interface import Interface

class IMyTheme(Interface):
    """Marker interface that defines a ZTK browser layer.
    """

In profiles/default/browserlayer.xml:

<layers>
  <layer
      name="my.theme"
      interface="my.theme.interfaces.IMyTheme"
      />
</layers>

In configure.zcml:

...
<!-- Grok the package -->
<grok:grok package="."/>
...

In covalent_member.py:

from five import grok
from ixds.covalent.behaviors.covalent_member import ActivateForm \
    as OriginalActivateForm
from my.theme.interfaces import IMyTheme

class ActivateForm(OriginalActivateForm):
    grok.layer(IMyTheme)

In covalent_member_templates/activateform.pt:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:i18n="http://xml.zope.org/namespaces/i18n"
      i18n:domain="my.theme"
      metal:use-macro="context/main_template/macros/master">

    <metal:block fill-slot="main">

        <h1 class="documentFirstHeading" tal:content="view/label | nothing" />

        <p>Hey there. I'd really like you to fill out this form.</p>

        <div id="content-core">
            <metal:block use-macro="context/@@ploneform-macros/titlelessform" />
        </div>

    </metal:block>

</html>

... and you will see the customised template rendered.

Upvotes: 3

Related Questions