Invenzzia » Resources / Articles / Managing XML in OPT / Managing tags

6. Managing tags

Another issue we are going to discuss is tag management. Here, OPT provides some appropriate tools, too. The basic one is opt:tag instruction:

<opt:tag name="$tagName">
  Some content

It allows to create dynamic tag with a selected name. The $tagName variable value may contain a namespace in an XML notation, but it may be also set separately with ns attribute. The second way has an advantage. If the expression defining the namespace is empty (its value is null), OPT knows not to prepend a colon to the tag name. Sometimes we may want our tag to contain a name attribute in the output. We add it with opt:attribute instruction.

The second useful tool is opt:on attribute. It basically works much like opt:if, but does not hide entire content, but just the tag. The content is always displayed. It can be seen in the action in the following example:

<p class="author"><span>Author:</span> <a parse:href="$homepage" opt:on="$homepage">{$nickname}</a></p>

If the user does not set a home page address, we do not want to create a hyperlink around his nickname, but the nickname itself still must be printed. Depending on the $homepage state (set or not set), we will get one of the following outputs:

<!-- when $homepage is set -->
<p class="author"><span>Author:</span> <a href="">User</a></p>
<!-- when $homepage is not set -->
<p class="author"><span>Author:</span> User</p>

In OPT 2.1 opt:on will be renamed to opt:omit-tag for the sake of clarity. Of course thanks to the automatic converter and the compatibility mode you do not have to worry about this change very much.