[Zope] strange 'print' behavior in Python Scripts

Michel Pelletier michel@digicool.com
Fri, 29 Jun 2001 19:28:55 -0700 (PDT)


On Fri, 29 Jun 2001, Christopher N. Deckard wrote:

> > From: "Christopher N. Deckard" <cnd@ecn.purdue.edu>
> > > Clarification, this happens when using triple quotes
> > >
> > > print '''
> > > foo
> > > bar
> > > '''
> > >
> > > Actually prints:
> > >  foo
> > >  bar
> >
> > Ah, I knew this would bite somebody someday, but you're the first to
> > mention it.  When a Script is compiled, it is actually converted into a
> > function by prefixing it with a "def f(params..." line.  If the body
> > isn't indented, it won't compile, so it indents it one space.  You can
> > get around this by manually indenting.  In other words:
> >
> >     ## Script (Python) "indented"
> >     print '''
> > foo
> > bar
> > '''
> >     return printed
>
> Hmm, this was not the answer I was looking for.  Are there any
> thoughts on fixing this in the future?  I've fun into it many times
> now and couldn't figure out what was going on.  It is especially bad
> when printing a textarea for a form.  Everything comes out indented
> a space.
>
> I'll just go line by line for now.  Thanks for the answer.

You can use DTML to format your XML:

<dtml-call "RESPONSE.setHeader('Content-Type', 'text/xml')">
<xml>
  <name><dtml-var name></name>
</xml>

You can do any logic necessary in python scripts, and call them from DTML.
DTML is meant for this task.

-Michel