是什么导致Genshi的模板语法错误?

时间:2022-06-20 18:32:57

A Genshi template raises the following error:

Genshi模板引发以下错误:

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

TemplateSyntaxError:“choose”指令的表达式“$ {item.error}”中的语法无效

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):

错误指定的模板代码部分如下('feed'是传递给模板的字典列表):

<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.

基本上,item.error持有'0'或'1',我希望输出基于此。我不确定错误在哪里 - 任何帮助表示赞赏。谢谢。

2 个解决方案

#1


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>

#2


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:

我从未使用过Genshi,但根据我发现的文档,看起来你试图在templates directives参数中使用内联Python表达式语法,这似乎是不必要的。试试这个:

<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>

#1


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>

#2


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:

我从未使用过Genshi,但根据我发现的文档,看起来你试图在templates directives参数中使用内联Python表达式语法,这似乎是不必要的。试试这个:

<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>