[Zope] Using ZPT with options/radio/checkboxes
Paul Winkler
pw_lists at slinkp.com
Thu Aug 7 12:54:37 EDT 2003
On Thu, Aug 07, 2003 at 01:59:34AM -0400, Ron Bickers wrote:
> <input type="checkbox" name="likespizza" value="1" tal:attributes="checked
> request/likespizza | nothing" />
>
> If you have a :list of checkboxes, you could use the following:
>
> <input type="checkbox" name="toppings:list" value="Pepperoni"
> tal:attributes="checked python:'Pepperoni' in path('request/toppings |
> python:[]') /> Pepperoni
(snip)
well, this is fascinating and very surprising to me.
I didn't see how your examples could work, until I tried them.
As I've always (mis)understood TAL, I thought that the following
bit of ZPT...
<input type="checkbox" attributes="checked python:0" />
<input type="checkbox" attributes="checked python:1" />
... should evaluate to the following HTML...
<input type="checkbox" checked="0" />
<input type="checkbox" checked="1" />
... and in both cases, Mozilla displays the checkbox as checked.
But in fact, this is not what happens!! The rendered HTML is:
<input type="checkbox" />
<input type="checkbox" checked="checked" />
Apparently, there is some special-casing in ZPT such that "checked"
and "selected" attributes are handled differently from all others.
The return value of the expression is tested for truth and then
discarded.
I must say that while this is convenient, it has 3 problems:
1) It's surprising - other attributes don't behave this way.
2) It's undocumented AFAICT.
3) It's specific to XHTML and likely to be problematic when using ZPT to
generate other flavors of XML. I notice that this special behavior
is applied regardless of what tag you put these attributes in.
I wonder if it would be better to have an explicit way that can
be used for any attribute, to conditionally insert or omit it.
There is of course the common idiom of writing the entire tag twice with two
opposing "tal:condition"s, but that often leads to redundant html
as well as redundant tag contents.
So it might be nice to be able to say "If this expression is true,
set the value of this attribute to the result; otherwise, omit
the attribute." Like so:
<input type="checkbox"
tal:conditional_attributes="checked request/some_var | nothing" />
Or, an alternative, there could be a way to say "remove the attribute
if this expression is true":
<input type="checkbox" checked="checked"
tal:omit_attributes="checked not:request/some_var | nothing" />
... or maybe spell it like this, to say "keep the attribute if this
expression is true":
<input type="checkbox" checked="checked"
tal:attribute_filters="checked request/some_var | nothing" />
I think I like that last variant the best, it seems easier to understand
than the first and usually more natural than the second.
It also combines nicely with existing tal:attributes useage:
<p
tal:attributes="class here/get_special_style"
tal:attribute_filters="class request/something | nothing" >
(As an optimization, tal:attribute_filters could be evaluated first,
and tal:attributes would not evaluate attributes that are to be filtered
out.)
P.S. okay Shane, fire away ;-)
--
Paul Winkler
http://www.slinkp.com
Look! Up in the sky! It's POST-MEGATRON-IAN!
(random hero from isometric.spaceninja.com)
More information about the Zope
mailing list