views:

298

answers:

2

A Genshi template raises the following error:

TemplateSyntaxError: invalid syntax in expression "${item.error}" of "choose" directive

The part of the template code that the error specifies is the following ('feed' is a list of dictionary which is passed to the template):

<item py:for="item in feed">
<py:choose error="${item.error}">
    <py:when error="0">
     <title>${item.something}</title>
    </py:when>
    <py:otherwise>
     <title>${item.something}</title>
    </py:otherwise>
</py:choose>
</item>

Basically, item.error holds either a '0' or a '1' and I want the output based on that. I am not sure where the error is - any help is appreciated. Thanks.

+1  A: 

I've never used Genshi, but based on the documentation I found, it looks like you're trying to use the inline Python expression syntax inside a templates directives argument, which seems to be unneccesary. Try this instead:

<item py:for="item in feed">
<py:choose error="item.error">
    <py:when error="0">
        <title>${item.something}</title>
    </py:when>
    <py:otherwise>
        <title>${item.something}</title>
    </py:otherwise>
</py:choose>
</item>
Jorenko
Thanks, Jorenko. I realized the mistake myself and changed it. But it still didn't work. I decided to use two if's instead and that worked.
Sam
+2  A: 

The docs perhaps don't make this clear, but the attribute needs to be called test (as it is in their examples) instead of error.

<item py:for="item in feed">
<py:choose test="item.error">
    <py:when test="0">
        <title>${item.something}</title>
    </py:when>
    <py:otherwise>
        <title>${item.something}</title>
    </py:otherwise>
</py:choose>
</item>
Chris Boyle