Search code examples
templatesplonegrokdexterity

Reuse the same template for different content types


I'm creating quite a few Dexterity content types (thanks zopeskel.dexterity devs!!) but even if I need them to be different content types (searches, collections...), some of them will be rendered equally.

So, there's any way to reuse the same template for different content types?

Ok, I made it work but I'm wondering if it's the correct approach:

from my.product.parent_type import IParentType, ParentType, TwoColumnsView

... code omitted ...

# Common folder for templates
grok.templatedir('parent_type_templates')

class SameTwoColumnsView(TwoColumnsView):
    grok.context(CustomClass)
    grok.require('zope2.View')

    grok.template("twocolumnsview")

Any thought? How do you reuse templates across content types?


Solution

  • Create an interface for this:

    from zope.interface import Interface
    
    class ITwoColumnViewable(Interface):
        """Can be viewed in a 2-column layout"""
    

    You then assign this interface to your various content types, and register the view for that interface instead directly for a type:

    class SameTwoColumnsView(TwoColumnsView):
        grok.context(ITwoColumnViewable)