Ternary-like operation in ZPT (updated)

Warning: blog abuse.  Just a permanent reminder to self.

A while back I asked Tres how to do a ternary-like operation in ZPT.  I needed to assign a value for a CSS class, where the value was one string in one case and another string for all other cases.

Tres gave me:

<div
class="blogEntry ${repeat['entry'].start and 'noborder' or ''}"
tal:repeat="entry entries">

(Updated July 15) Malthe wrote in a comment that he just changed Chameleon to allow the ternary syntax of ${'foo' if bar else 'boo'} ). Look in the sourcecodegen 0.6.11 release.)

9 Responses to “Ternary-like operation in ZPT (updated)”

  1. Wichert Akkerman Says:

    If you use ZPT with python 2.5 or later you can also use the new python ternary statement: ${‘noborder’ if repeat[‘entry’].start else ”}

  2. David Glick Says:

    Just beware if the thing between the ‘and’ and the ‘or’ evaluates to boolean false (such as an empty string), because then you’ll *always* get the second option.

  3. Wichert Akkerman Says:

    It is not a chameleon-ism: this is a new syntax that was introduced in python 2.5. You can use it everywhere you use python.

  4. Malthe Says:

    Actually, let me implement this in “sourcecodegen“ (the IfExp-clause).

  5. Malthe Says:

    Now available with sourcecodegen 0.6.11.

  6. Marius Gedminas Says:

    Can you actually do that in ZPT? I always though you needed to write the full

    tal:attributes=”class python:’blogEntry’ + repeat[‘entry’].start and ‘ noborder’ or ””

    BTW are you aware that your blog feed on Planet Python is broken? The posts are truncated and link to http://planet.python.org instead of permalinking to the post in question, making it *very* hard to read what you say.

    • Paul Everitt Says:

      I got another report that the feed is broken on Planet Python. I’m using hosted WordPress with few changes, so I can’t really imagine what is the problem.

  7. Why does Planet Python bork my WordPress links? « Chatterbox, Reloaded Says:

    […] (and others) reported that Planet Python screws up the links on my articles.  Instead of a full link to the article, it […]

Leave a reply to Why does Planet Python bork my WordPress links? « Chatterbox, Reloaded Cancel reply