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.)
July 14, 2009 at 2:50 pm |
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 ”}
July 14, 2009 at 2:55 pm |
Is that a Chameleon-ism also?
July 14, 2009 at 4:34 pm |
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.
July 14, 2009 at 11:04 pm |
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.
July 15, 2009 at 7:38 am |
Actually, let me implement this in “sourcecodegen“ (the IfExp-clause).
July 15, 2009 at 7:42 am |
Now available with sourcecodegen 0.6.11.
July 20, 2009 at 1:02 am |
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.
July 20, 2009 at 1:08 am |
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.
July 22, 2009 at 2:56 pm |
[…] (and others) reported that Planet Python screws up the links on my articles. Instead of a full link to the article, it […]