[Zope] Re: Filling a metal slot which name is known only at run time

Martin Aspeli optilude at gmx.net
Tue Dec 14 12:41:52 EST 2004


> Martin Aspeli wrote:
>> I'm creating a content type which composes the views of several  
>> objects  (the exact objects are picked by the end user) into a single  
>> view.  Developers can write a page template with a macro called   
>> "CompositeListing" and register this template with a tool. Once   
>> registered, users can pick the template as a layout for the composite   
>> listing page.
>
> I'm having trouble grasping this, or at least what the content object  
> *does* with the macro.  What is it filling the slots with, and how does  
> it decide which slots to fill with what, if the slots aren't uniform  
> from template to template?

Consider a content type CompositePage. A user selects one from a number of  
pre-defined layouts. Each layout contains slots (both in the Zope sense  
and the logical sense) where composited content may appear. The user then  
selects which content objects (from elsewhere in the site) to put in which  
of these slots.

It is important that creating a new layout is as simple as possible.  
Hence, I do this by allowing layout creators to write a ZPT which contains  
metal:define-slot statements where the slots should go (e.g. inside a <td>  
or <div>). The ZPTs containing the macro is registered with a tool, which  
the CompositePage queries to let the user choose a layout.

This would be a simple layout template:

<div metal:define-macro="layout">
	<div metal:define-slot="top_slot"/>
	<hr/>
	<div metal:define-slot="bottom_slot"/>
</div>

When viewing the CompositePage, it will do the following:

<metal:layout use-macro="here/getChosenLayoutTemplate">
	<metal:composite fill-slot="top_slot">
		<div tal:content="structure python:here.renderContentForSlot  
('top_slot')"/>
	</metal:composite>
	<metal:composite fill-slot="bottom_slot">
		<div tal:content="structure python:here.renderContentForSlot  
('bottom_slot')"/>
	</metal:composite>
</metal:layout>

When editing the CompositePage, it will instead of rendering the content  
render a widget from which the user can choose which item to place in a  
given slot. This selection is then saved inside the CompositePage, which  
looks the item up and renders it (via a special view template) so that it  
may be shown with the tal:content statement above.

Does that make more sense?

I have implemented this with slot-names hardcoded to slot0, ..., slot9.  
However, I'd much prefer if the CompositePage macro would just pick up the  
various slots defined inside the layout, so that template authors could  
use more meaningful names and potentially use more than 10 slots. I have a  
script which lets me get the list of slot names defined inside the macro  
"layout" in the chosen layout template. The problem, as stated before, is  
that I can't just tal:repeat loop over this and dynamically use  
metal:fill-slot, as metal:fill-slot appearently only accepts static  
strings, not TAL/Python statements.

Martin

-- 
"Life is both a major and a minor key" -- Travis, Side



More information about the Zope mailing list