[Zope3-Users] Re: How to render PageTemplate manually?
Pawel Lewicki
lewicki at provider.pl
Thu Jan 5 07:25:11 EST 2006
Łukasz Łakomy napisał(a):
> Hi all
>
> My use case:
> Create content type 'Portlet' that could have TAL as its content and when
> watched in browser TAL should be redered. My code is below.
>
> class PortletView:
> def renderText(self):
> portlet = IPortlet(self.context)
> format = portlet.format
> if format in ('HTML', 'Plain'):
> return portlet.getText()
> elif format == 'Structured':
> struct = structurize(portlet.getText())
> doc = DocumentWithImages()(struct)
> output = HTMLWithImages()(doc, level=1)
> return output
> elif format == 'Page Template':
> page_template = PageTemplate()
> page_template.write(portlet.getText())
> output = page_template()
> return output
>
> But there is a problem. When I define a variable in template and call it its
> ok. But I cannot call 'context' variable (for example <div
> tal:content="context/title"/>). This is my traceback.
>
> Traceback (most recent call last):
....
Apply the snippet to your needs (provide proper 'context', etc.).
Powodzenia ;)
If anyone knows simpler solution please share.
Pawel
--------------------------------------------
from TAL.TALInterpreter import TALInterpreter
from Products.PageTemplates.Expressions import getEngine
from Products.PageTemplates.PageTemplate import PageTemplate
from cStringIO import StringIO
data = {
'test': 'Testing text',
}
t1='<h1 tal:content="test"></h1>'
context = getEngine().getContext(data)
buffer = StringIO()
pt = PageTemplate()
pt.write(t1)
pt._cook()
TALInterpreter(pt._v_program, pt._v_macros, context, buffer)()
macro = buffer.getvalue()
print macro
More information about the Zope3-users
mailing list