Question

How do I do a simple if/else in the Genshi templating language?

I've found this ticket, which seems to suggest that Genshi doesn't support if/else, but it doesn't really explain what it supports instead.

I basically just want something like this:

  <py:if test="c.row.currency">
  ${c.row.currency.upper()}
  <py:else>
  ${c.row.dataset_.currency.upper()} 
  </py:if>

But I get 'Bad Directive: else'. Should I be using py:choose instead? I can't really get my head around how to use it for an if/else condition.

Was it helpful?

Solution

Currently, you can not if do else constructs in Genshi, and as far as I'm aware, there are no plans to add it. Instead, like you mentioned, use py:choose. The following is how you use py:choose as a type of if/else construct:

<py:choose ...>
  <py:when test="...">
    ${c.row.currency.upper()}
  </py:when>
  <py:otherwise>
   ${c.row.currency.upper()}
  </py:otherwise>
</py:choose>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top