>
> Hi,
>
> I believe the XHTML Strict standard says that boolean attibutes such
> as readonly should be present when true and totally absent when false.
Yes, I believe this is the case.
> As far as I can see this means that I must resort to conditionals like
> py:if or py:choose to correctly render elements that contain boolean
> attributes. e.g:
>
> <py:choose test="readonly == True">
> <input py:when="True" readonly="readonly" type="text"/>
> <input py:otherwise="" type="text"/>
> </py:choose>
>
> All well and good for trivial elements but very cumbersome for
> elements with larger numbers of attributes.
>
> Is there a more succinct way of achieving this without duplicating the
> majority of the element?
>
Have you tried py:attrs?
<input py:attrs="{'readonly': 'readonly'} if condition else {}" />
Not tested :) .. Basically its value is some expression that is expected
to evaluate to a dict, whose key/value pairs are then applied to the
element as attribute name/value pairs.
>
> Thanks in advance,
>
> t o b e
-Kyle
Why not simply readonly="${readonly or None}"?
Genshi renders readonly="readonly" for every true value readonly.
-- Christoph
I didn't know Genshi had this behavior. Does this work for attributes in
general, or just "boolean" ones defined for HTML?
-Kyle
It's the same behavior as in the Kid templating language.
The feature that a value of None removes the attribute works for every
attribute. That a true value sets the attribute to the attribute name
happens only with the XHTML serializer and with boolean attributes.
Similarly, if you use the HTML serializer, you get just 'readonly'
instead of 'readonly="readonly"'.
-- Christoph