tal:attributes

This attribute changes tag attribute(s) value(s).

<a href="http://www.foo.com" title="some foo link"
   tal:attributes="href somelink/href; title somelink/title"
  tal:content="somelink/text"
>sample link</a>

With a 'somelink' having:

$somelink->href = "http://www.google.com";
$somelink->title = "google search engine";
$somelink->text = "the google search engine";

Will produce:

<a href="http://www.google.com"
title="google search engine">the google search engine</a>

Semicolon (;) separates attributes. If you want semicolon to be output in an attribute, you have to double it (;;).

A somewhat complicated example involving tal:repeat:

<tr tal:repeat="ranking playerRankings"
    tal:attributes="class php: repeat.ranking.odd ? 'odd' : NULL"></tr>

The php: modifier will be explained later, basically if the line is odd then tr will have a class attribute with "odd" as value, otherwise, no class will be set.

The "condition ? then : else" is a regular PHP expression which must be used with care but has proven to be useful on more than one occasion.

A better way to achieve the same result would be to ask your PHP coder to create a custom modifier for your needs (see PHP integration / custom modifiers) which would be used as follows:

<tr tal:repeat="ranking playerRankings"
    tal:attributes="class css-odd:repeat/ranking/odd"></tr>

The modifier would return "odd" if repeat/ranking/odd is true, NULL otherwise.

Optional attributes

If you use TALES alternatives in tal:attributes and use nothing (or NULL in PHP) as last alternative, attribute won't be added at all if there's no value for it (this avoids adding empty attributes):

… tal:attributes="title object/tooltip | nothing"> 

XHTML attributes like selected, checked, etc. are properly handled automatically.

<input type="checkbox" tal:attributes="checked object/isChecked"/>

Warning

Remember that XHTML is case-sensitive, so SELECTED attribute is an error in XHTML. Use selected.