Discussion:
using return value of function/generator
Oni
2010-12-08 02:09:50 UTC
Permalink
The below produces 'Hello' which is great:

<p py:def="greeting()" py:strip="True">Hello</p>

<div py:choose="">
<span py:when="0 == len('%s' % greeting)">nothing</span>
<span py:when="0 != len('%s' % greeting)">${greeting()}</span>
<span py:otherwise="">otherwise</span>
</div>

However the below returns blank; but I would like it to return the
text 'nothing' or 'otherwise'.

<p py:def="greetingtwo()" py:strip="True"></p>

<div py:choose="">
<span py:when="0 == len('%s' % greetingtwo)">nothing</span>
<span py:when="0 != len('%s' % greetingtwo)">${greetingtwo()}</
span>
<span py:otherwise="">otherwise</span>
</div>

The interesting thing is

<div py:with="x=greeting()">
"${x}" <br />
"$x" <br />
</div>

Returns:
"Hello"
""

But when I try to use $ or ${..} in the below genshi comes up with
errors

<div py:choose="">
<span py:when="0 == len('%s' % $greetingtwo)">nothing</span>
<span py:when="0 != len('%s' % $greetingtwo)">${greetingtwo()}</
span>
<span py:otherwise="">otherwise</span>
</div>

Any insights or handy hints appreciated
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
Kyle Alan Hale
2010-12-10 00:34:29 UTC
Permalink
I believe it is because you forgot the parens in the function calls in the
py:when tests. In other words, if you update the second example to be

<p py:def="greetingtwo()" py:strip="True"></p>

<div py:choose="">
<span py:when="0 == len('%s' % greetingtwo())">nothing</span>
<span py:when="0 != len('%s' % greetingtwo())">${greetingtwo()}</
span>
<span py:otherwise="">otherwise</span>
</div>

it should evaluate as you'd expect. The fact that the first example worked
as you'd expected was only coincidence, since

'%s' % greeting == str(greeting) # True
Post by Oni
<p py:def="greeting()" py:strip="True">Hello</p>
<div py:choose="">
<span py:when="0 == len('%s' % greeting)">nothing</span>
<span py:when="0 != len('%s' % greeting)">${greeting()}</span>
<span py:otherwise="">otherwise</span>
</div>
However the below returns blank; but I would like it to return the
text 'nothing' or 'otherwise'.
<p py:def="greetingtwo()" py:strip="True"></p>
<div py:choose="">
<span py:when="0 == len('%s' % greetingtwo)">nothing</span>
<span py:when="0 != len('%s' % greetingtwo)">${greetingtwo()}</
span>
<span py:otherwise="">otherwise</span>
</div>
The interesting thing is
<div py:with="x=greeting()">
"${x}" <br />
"$x" <br />
</div>
"Hello"
""
But when I try to use $ or ${..} in the below genshi comes up with
errors
<div py:choose="">
<span py:when="0 == len('%s' % $greetingtwo)">nothing</span>
<span py:when="0 != len('%s' % $greetingtwo)">${greetingtwo()}</
span>
<span py:otherwise="">otherwise</span>
</div>
Any insights or handy hints appreciated
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To unsubscribe from this group, send email to
.
For more options, visit this group at
http://groups.google.com/group/genshi?hl=en.
--
You received this message because you are subscribed to the Google Groups "Genshi" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to genshi+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/genshi?hl=en.
Loading...