Skip to content.

plope

Personal tools
You are here: Home » Members » slinkp's Home » Meld2
 
 

Meld2

A prototype of the next generation super-simple templating system, based on Richie Hindle's PyMeldLite.

The code, such as it is, is available at http://www.slinkp.com/code/zopestuff/templates/Meld2.py ... feedback welcome!

It requires PyMeldLite.py, which is available as part of SpamBayes

Note, Meld2.py is currently tested only with Python 2.4.

Check the docstrings for usage examples.

Rationale at http://www.slinkp.com/code/zopestuff/templates/

Created by slinkp
Last modified 2005-11-28 10:29 AM

nice...

This is really quite cool. I dig the namespace'd ids. It'd be useful to get some examples outside the context of the docstrings.

Here's one:

>>> print text
<div meld:id="outer">
<span meld:id="orange" style="color: orange;">
<span meld:id="inner">
<p meld:id="orange">Fruity!</p>
</span>
<span meld:id="inner2">
<p meld:id="orange">Chewing gum</p>
</span>
</span>
</div>

>>> xml = ['<yeah/>', '<all/>', '<right/>']
>>> data = Meld2(text)
>>> g = data.outer.repeat(xml)
>>> for thing, info in g: thing.inner = info
...
>>> print data
<div meld:id="outer/0">
<span meld:id="orange" style="color: orange;">
<span meld:id="inner"><yeah/></span>
<span meld:id="inner2">
<p meld:id="orange">Chewing gum</p>
</span>
</span>
</div><div meld:id="outer/1">
<span meld:id="orange" style="color: orange;">
<span meld:id="inner"><all/></span>
<span meld:id="inner2">
<p meld:id="orange">Chewing gum</p>
</span>
</span>
</div><div meld:id="outer/2">
<span meld:id="orange" style="color: orange;">
<span meld:id="inner"><right/></span>
<span meld:id="inner2">
<p meld:id="orange">Chewing gum</p>
</span>
</span>
</div>
>>>

suh-weet

Thanks much for coding this.

Probably my main criticism is overloading __getattr__ for subelements. IMHO, subelements should be accessible via __getitem__ and attributes via __getitem__ on a dict-like subobject such as "attr". I guess after Zope's gross overloading of __getattr__ I have an allergy to it. Besides, it is confusing having elements and methods in the same namespace.

So essentially, __getitem__ would replace getChild() and self.attr.__getitem__ would replace the current __getitem__. Takers?

+1

.. I think. Although it's pretty nice to be able to use setattr and getattr instead of get/setitem, and i think zope's problem actually reolves more around making the results the *same*, it's cleaner to disambiguate "methodspace" from element-space entirely.

Macro-style reuse example

Here's an example that shows how easy it is to do the equivalent of define-macro/use-macro and define-slot/fill-slot. I suspect we may want to define a method for doing this, it could
get slightly tedious if there are a lot of slots.
You can download this from http://slinkp.com/code/zopestuff/templates/examples.tgz

#### template one, save as "reuse_main.xhtml" ##############################
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<!-- An example of template reuse, a la ZPT macros.
This is a template that will be used by others. -->

<span meld:id="master_macro">

<h1 meld:id="title_slot">
Default Title to be replaced
</h1>

<h2>This is in main template and is part of the "macro"</h2>

<p>This is also in main template and is part of the "macro"</p>

</span>

<p>This content is in main template but is not inside the "macro".</p>

</body>
</html>

####### template 2, save as "reuse_derived.xhtml" ##########################
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<!-- An example of template reuse, a la ZPT macros.
This is a template that will re-use and fill the main template. -->
<span meld:id="master_macro">
<h1 meld:id="title_slot">
<i>Hello from the derived page</i>, this will be inserted in the output.
</h1>

<p>The main template's content will go here, and this
dummy content will go away.
</p>

</span>

<p>Here's some content in the derived page that appears after the "macro".
</p>

</body>
</html>

################ Driving script, save as "reuse.py" ###########################

from Meld2 import Meld2

main = Meld2(open('reuse_main.xhtml').read())
derived = Meld2(open('reuse_derived.xhtml').read())

# First, copy of anything we want to fill "slots" with.
title = derived.title_slot
# Next, insert the "macro".
derived.replaceNode('master_macro', main.master_macro)
# Populate "slot" with the saved content.
derived.replaceNode('title_slot', title)



###################### ANd here's the output. ##############################################

<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<!-- An example of template reuse, a la ZPT macros.
This is a template that will re-use and fill the main template. -->
<span meld:id="master_macro">

<h1 meld:id="title_slot">
<i>Hello from the derived page</i>, this will be inserted in the output.
</h1>

<h2>This is in main template and is part of the "macro"</h2>

<p>This is also in main template and is part of the "macro"</p>

</span>

<p>Here's some content in the derived page that appears after the "macro".
</p>

</body>
</html>