[TransWarp] More PWT Ideas (usage, not terminology!)

Roché Compaan roche at upfrontsystems.co.za
Sat Jul 26 06:00:11 EDT 2003


* Phillip J. Eby <pje at telecommunity.com> [2003-07-25 23:53]:
> Ironically, I think the trouble you're having with understanding PWT is 
> that it's a lot *simpler* than you think.  :)  DOMlets are the only thing 
> there *is*, it's just that the domlet="" attribute lets you override the 
> default choice of what *kind* of DOMlet is used for that element.

Mmm, I see now. But, third time lucky :-) I roughly documented my walk
through the template parsing and rendering part of the code - it might
help others to get into it faster. I'll do the same for interaction,
traversal and publishing.

document.parseFile(StringIO("""<something 
domlet="text:theVar">whatever</something>"""))

DomletParser parses the template and creates a 'text' DOMlet with
'dataSpec' set to 'theVar'. This happens in the startElementHandler
'startElement':

    if domletName:
        factory = DOMLETS_PROPERTY.of(top)[domletName]

DOMLETS_PROPERTY is a configuration property. This means you can
easily extend the list of DOMlets for your app in a config file by
defining a [peak.web.DOMlets] section. In peak.ini 'text' and 'list' are
listed as DOMlets for this property.

If we evaluate 'factory' for the 'something' element in our template it
returns:

    <class 'peak.web.templates.Text'>

We use the factory to create the element and append it to the list of
domlets and the stack maintained by the parser.

If the parser sees the "define" keyword in the attribute (as is required
for 'list' domlets) of a tag, eg.

    <ul domlet="list:data">
        <li define="listItem">item value</li>
    </ul>

it appends a parameter to the domlet:

        if paramName:
            self.domlets[-1].addParameter(paramName,element)

In the endElement handler the element created by factory is added as a
child of the last element in the stack - its parent node. 'addChild' is
a method of the Element class - the base for all DOMlets.

        last = self.stack.pop()
        self.stack[-1].addChild(last)

We call document.renderFor(data, state) in our app to render the
template. 'data' is an instance of a class that implements the
IWebTraversable interface. It should define 'theVar' that
will be used to replace the content of the 'something' element.

TemplateDocument inherits 'renderFor' from Element. It iterates over
optimizedChildren and calls the 'renderFor' method for each child
element. In the simple template above 'document' has only child, a text
domlet. The 'renderFor' method of text replaces the content of the
element. It then uses the write function from the state instance to
write the opening tag, the content for the tag and finally the closing
tag.


-- 
Roché Compaan
Upfront Systems                 http://www.upfrontsystems.co.za



More information about the PEAK mailing list