sebb | 13 Jun 12:13
Picon

Best way to handle optional attributes in 1.6.2

I'm generating HTML using a Velocity style sheet with version 1.6.2

The source contains lots of elements which are used to generate table
cell entries:

<property name="Name" required="">Description</property>

The "required" attribute is supposed to be "Yes" or "No", with a
default of "No".

The VSL currently has:

<td>
#if($items.getAttributeValue("required") != "")
$items.getAttributeValue("required")
#else
No
#end
</td>

This works OK provided that the "required" attribute is present,
however I would like to treat a missing attribute the same way as the
empty string.

The above code used to work in Velocity 1.5, but now I get

<td>$items.getAttributeValue("required")</td>

instead of

<td>No</td>

The following works, but seems rather messy:

<td>
#if($items.getAttributeValue("required") &&
$items.getAttributeValue("required") != "")
$items.getAttributeValue("required")
#else
No
#end
</td>

Is there a better way to handle optional attributes?

Gmane