Next / Previous / Contents / Shipman's homepage

13.2. CLASS(): Adding class attributes

One of the commonest operations is to attach a class attribute to an XML tag. For instance, suppose you want to generate this content:

<div class='warning'>
  Your brain may not be the boss!
</div>

The obvious way to do this does not work:

  E.div("Your brain may not be the boss!", class='warning')  # Fails!

Because class is a reserved word in Python, you can't use it as an argument keyword. Therefore, the package includes a helper function named CLASS() that takes one or more names as arguments, and returns a dictionary that can be passed to the E() constructor to add a class= attribute with the argument value. This example does work to generate the above XML:

  E.div("Your brain may not be the boss!", CLASS('warning'))  # Works.

Here's another example, this time with multiple class names.

   E.span(CLASS('ref', 'index'), "Pie, whole.")

This generates:

  <span class='ref index'>Pie, whole.</span>