[Zope] DTML rendering from python?

Dylan Reinhardt Dylan@DylanReinhardt.com
Thu, 23 Jan 2003 07:58:54 -0800


At 07:02 AM 1/23/2003, Michiel Toneman wrote:

>Hi Dylan, hi Andy
>
>Great answers, very true and also useful, thanks. "Don't do it that
>way!" has in the past been some of the best advice given to me.

Me too.  Let's see if it bears out in this case...


>(Un?)Fortunately, I just put the "<p><dtml-var id></p>" in there as an
>example to simplify the explanation of my problem, and (I think) I have
>a valid reason for wanting to do this. (I'd love some feedback on this.
>If you decide I'm nuts anyway, I'll probably have agree with you ;-) )

Sure.  One of the most frustrating things about these lists is when you put 
up a trivial example of a complex problem and then you get responses to the 
example, not the issue itself.


>What I'm actually doing is building a Content Management system for
>which the layouts are not pre-defined.

OK....


>The content manager edits content
>using structured text. The getHTML function takes the structured text,
>translates this into a HTML snippet. This part is easily achieved in
>DTML only.

Yep.


>Now it gets tricky, though. I need to insert small "info
>boxes" into the html. These are "floated" just after a paragraph tag.
>Not all paragraphs have such a floating info-box, this is selectable by
>the content manager.
>
>The HTML might look like this:
>
>----------------------------------------------------------
><h2>Lorem ipsum dolor sit elitr</h2>
>   <p>
>     <div style="float:right; width: 150px;">
>       <img src="images/voorbeeld4.jpg" alt="voorbeeld" /><br />
>       Lorem ipsum dolor
>     </div>
>     Sed diam nonumy eirmod tempor invidunt ut labore et
>     dolore magna aliquyam erat, sed diam voluptua.
>   </p>
>----------------------------------------------------------
>
>where the "div" is opional. This is the part that DTML can't handle.


<div<dtml-if is_floater> style="float:right; width: 150px;"</dtml-if>>
    stuff
</div>


>To do this, I feed the HTML Snippet to the DOM parser, locate all the
>paragraphs, and insert a "dtml-var" tag just after the "p" tag where
>ever a "floating box" should be present.

Or you could use CSS.  Label all the sections and use DTML to dynamically 
implement the appropriate behavior in the style sheet.


>I have thus preprocessed the structured-text into resultant DTML, for
>which I'd like to let the DTML renderer do the heavy lifting.

Wouldn't the easiest thing be to have the DTML document retrieve the 
structured text it needs and render it?  Ex:

<dtml-var some_text fmt="structured-text">

>I was
>hoping that simply rendering getHTML in a DTML document would do the
>trick, but the return value is output "raw" into the DTML document.

Yes, that's because rendering getHTML into text is all that's done when the 
DTML is processed.  It won't recurse into the results to look for further 
instructions.  That's a *good* thing.


>My "real" code therefore looks like:

<snip>

>         html = "<div>" + HTML(text, level=2, header=0) + "</div>"

Why not:

(in Python)
def get_text(self):
     return self._htmltxt

(in DTML)
<div>
     <dtml-var "get_text()" fmt="structured-text">
</div>

If you want the behavior of your div to be controlled directly by the 
product, how about:

(in Python)
_layouts = {
                   'layout1': 'float:right; width: 150px;',
                   'layout2': 'width: 150px;'
                 }
def div_layout(self, some_condition):
     if some_condition == some_other_thing:
         return self._layouts['layout1]
     else:
         return self._layouts['layout2']

(in DTML)
<div <dtml-var "div_layout(some_info)">>
    <dtml-var "get_text()" fmt="structured-text">
</div>

Obviously, I'm inferring additional logic and data.... but does that help 
at all?

Dylan