Skip to content

Instantly share code, notes, and snippets.

@simahawk
Last active August 29, 2015 14:00
Show Gist options
  • Save simahawk/192d92b77437e6d4cc0a to your computer and use it in GitHub Desktop.
Save simahawk/192d92b77437e6d4cc0a to your computer and use it in GitHub Desktop.
A dexterity advanced page that renders ZPT markup
#-*- coding: utf-8 -*-
from Acquisition import aq_inner
from Acquisition import aq_base
from five import grok
from zope import schema
from zope.component import getMultiAdapter
from plone.dexterity.content import Container
from plone.directives import form
from plone.namedfile.interfaces import IImageScaleTraversable
from plone.outputfilters.interfaces import IFilter
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
# fake message factory
_ = lambda x: x
# Interface class; used to define content-type schema.
class IAdvancedPage(form.Schema, IImageScaleTraversable):
"""
An advanced page with some goodies
"""
body = schema.Text(
title=_(u"Body"),
description=_(
u"Fill in the body of the page. Supports TAL templating."
),
required=True,
default=u""
)
class AdvancedPage(Container):
grok.implements(IAdvancedPage)
# Add your class methods and properties here
def body_compile(self):
if not self.body:
return ''
pt = ZopePageTemplate(id='__adv_page_tal_body__')
pt.pt_edit(self.body, 'text/html')
context = aq_inner(self)
pt = aq_base(pt).__of__(context)
# request is taken by acquisition
return pt()
class AdvancedPageView(grok.View):
"""AdvancedPage view class """
grok.context(IAdvancedPage)
grok.require('zope2.View')
grok.name('view')
def body(self):
text = self.context.body_compile()
# encode text before to resolve uids
if isinstance(text, unicode):
text = text.encode('utf-8')
# trasform resolveuid link to absolute path
_filter = getMultiAdapter(
(self.context, self.request),
IFilter, name="resolveuid_and_caption")
try:
text = _filter(text)
except AttributeError:
# Something wrong happend
# perhaps image scale doesn't exists
pass
return text.decode('utf-8')
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
lang="en"
metal:use-macro="context/main_template/macros/master"
i18n:domain="my.package">
<body>
<metal:main fill-slot="main">
<div tal:replace="structure view/body">The body of the page </div>
</metal:main>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment