Boolean attributes in XHTML with Genshi

166 views
Skip to first unread message

t o b e

unread,
Apr 21, 2009, 1:18:42 PM4/21/09
to Genshi
Hi,

I believe the XHTML Strict standard says that boolean attibutes such
as readonly should be present when true and totally absent when false.
My main development browser, Firefox 3.0.8 on Ubuntu seems to support
this. e.g:

<input type="text" value="some_text"/> <-- Read/Write
<input readonly="readonly" type="text" value="some_text"/> <--
ReadOnly
<input readonly="" type="text" value="some_text"/> <-- ReadOnly also
(but not strictly correct ??)

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?

This would be ideal:

<?python readonly = some_condition and "readonly='readonly'" or "" ?>
<input ${readonly} type="text" value="some_text"/>

But sadly doesn't work.. invalid token.

Thanks in advance,

t o b e





Kyle Schaffrick

unread,
Apr 21, 2009, 4:07:17 PM4/21/09
to gen...@googlegroups.com
On Tue, 21 Apr 2009 10:18:42 -0700 (PDT)
t o b e <toby.b...@gmail.com> wrote:

>
> 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

kindy

unread,
Apr 21, 2009, 8:41:06 PM4/21/09
to gen...@googlegroups.com
On Wed, Apr 22, 2009 at 4:07 AM, Kyle Schaffrick <ky...@raidi.us> wrote:
>
> On Tue, 21 Apr 2009 10:18:42 -0700 (PDT)
> t o b e <toby.b...@gmail.com> wrote:
>
>>
>> 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>

<input readonly="${1 if readonly else None}" type="text"/>

this is enough.

genshi will auto find the boolean attributes, and drop it if it set to None,

<input readonly="${True if readonly else None}" checked="${True if
checked else None}" type="text"/>

>>
>> 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
>
> >
>



--
Regards,

Lin Qing

Toby Bradshaw

unread,
Apr 22, 2009, 7:12:35 AM4/22/09
to gen...@googlegroups.com
Thanks for your suggestions.

> <input readonly="${1 if readonly else None}" type="text"/>

Didn't actually work for me. Syntax error (Genshi 5.0.2, Python 2.4.2).

The simplest construct I did find that works for me is:

<input readonly="${readonly and True or None}" type="text/>

Which is perfect for my needs. I must have missed (or it's absent)
that point in the docs about attribs == None being stripped.

Thanks again, folks.

--
t o b e


2009/4/22 kindy <kin...@gmail.com>:

Matt Good

unread,
Apr 22, 2009, 9:06:37 PM4/22/09
to Genshi
On Apr 22, 4:12 am, Toby Bradshaw <toby.brads...@gmail.com> wrote:
> Thanks for your suggestions.
>
> > <input readonly="${1 if readonly else None}" type="text"/>
>
> Didn't actually work for me. Syntax error (Genshi 5.0.2, Python 2.4.2).

Yeah, the other post uses the if/else construct added in 2.5, so it
won't work in earlier versions.

> The simplest construct I did find that works for me is:
>
> <input readonly="${readonly and True or None}" type="text/>
>
> Which is perfect for my needs. I must have missed (or it's absent)
> that point in the docs about attribs == None being stripped.
>
> Thanks again, folks.
>
> --
> t o b e
>
> 2009/4/22 kindy <kind...@gmail.com>:

Christoph Zwerschke

unread,
Apr 23, 2009, 4:47:20 AM4/23/09
to gen...@googlegroups.com
Matt Good schrieb:

> Yeah, the other post uses the if/else construct added in 2.5, so it
> won't work in earlier versions.

Why not simply readonly="${readonly or None}"?

Genshi renders readonly="readonly" for every true value readonly.

-- Christoph

Kyle Schaffrick

unread,
Apr 23, 2009, 10:52:25 AM4/23/09
to gen...@googlegroups.com

I didn't know Genshi had this behavior. Does this work for attributes in
general, or just "boolean" ones defined for HTML?

-Kyle

Christoph Zwerschke

unread,
Apr 23, 2009, 1:06:11 PM4/23/09
to gen...@googlegroups.com
Kyle Schaffrick schrieb:

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

Reply all
Reply to author
Forward
0 new messages