plonetemplate-talzpt

Doing <input disabled="disabled"/> or other boolean attribute in TAL


How one does attributes which may or may not exist? Exampls are checkbox checked="checked" or disabled="disabled" HTML attributes.

What kind of tal:attributes expression is involved?


Solution

  • When an attribute listed in tal:attributes is set to None, the attribute is omitted:

    <span tal:attributes="title python:len(item['title']) < 10 and item['title'] or None">
       Only a title if shorter than 10 characters.
    </span>
    

    The same applies to a path expression to a non-existing object:

    <span tal:attributes="title item/title" />
    

    Now the title attribute will only be set if there is a title key or attribute on item. Note that if item/title resolves to None, the attribute is omitted as well.

    From the TALES 1.4 specification, attributes section:

    If the expression associated with an attribute assignment evaluates to nothing, then that attribute is deleted from the statement element.

    Where python None is interpreted as "nothing".