[Zope3-dev] Re: [DRAFT] local portlets and perspectives

Tonico Strasser contact_tonico at yahoo.de
Tue Aug 30 10:27:46 EDT 2005


Raphael Ritz schrieb:
> Tonico Strasser wrote:
> 
>> [..]
>>
>> If yes, I mean something different. I think I would love a Python 
>> wrapper for Page Templates that can assemble *macros* from different 
>> templates provide them with names and render the main template only 
>> once. (Wait, that is already possible, isn't it?)
> 
> 
> Maybe I didn't get your question but isn't that exactly what Archetypes
> does to render widgets?

Didn't look into Archetypes, let's see.

> 
> Example (from: http://docs.neuroinf.de/programming-plone/ate#2-18)
> 
> <metal:data use-macro="python:obj.widget(field_name)" />

That looks interesting, the 'widget' returns a macro?

> where 'widget' is defined in BaseObject
> (https://svn.plone.org/svn/archetypes/Archetypes/trunk/BaseObject.py)
> 
>    security.declareProtected(CMFCorePermissions.View, 'widget')
>    def widget(self, field_name, mode="view", field=None, **kwargs):
>        if field is None:
>            field = self.Schema()[field_name]
>        widget = field.widget
>        return renderer.render(field_name, mode, widget, self, field=field,
>                               **kwargs)

Hm, the term render implies something rendered, not a macro definition.

> That way you can render a field's value for a given object wherever you 
> like.

If I understand that correctly I get a unrendered macro?

> For this example the macro(s) come(s) from the template configured on
> the widget but it shouldn't be too hard to generalize that if needed.
> 
> But my gutt feeling says I missinterpret your question ...

That is related to what I want.

I've already built my own Z2 Python scripts to do the following:

##

# a template
main_template = context.main_template

# a page object that will render the main_template
page = context.Page(template=main_template)

# a macro provider
view = context.document_view

# a macro mapping which I can update with various macros
page.macros.update(view.macros)

# assign random names to the namespace
page.foo = 'bar'

# render the page
return page.render()

##

This is my Page object:
...
##parameters=template, **kw
##
from Products.PythonScripts.standard import Object

page = Object()
page.request = container.REQUEST
page.response = page.request.RESPONSE
page.template = template
page.macros = template.macros.copy()
page.update_macros = page.macros.update
page.update(kw)

def render(*args, **options):
     options['args'] = args
     page.options = options
     page.here = context
     page.context = context
     return page.template.pt_render(extra_context=page)

page.render = render

return page

##

Very primitive, I know :)

Tonico



More information about the Zope3-dev mailing list