Dynamator Pure HTML for every page generation technology.
           

Dynamic Attribute

This lesson shows how to manipulate HTML attributes. This capability is essential for forms handling.

Dynamator allows attributes for any HTML element to be added, changed, or removed.

The example used in this lesson is somewhat bogus, because it uses attributes and elements intended for formatting where a modern HTML UI would use CSS. The example is designed to make it easy to understand Dynamator behavior, not to be state-of-the-art HTML.

We start with our original Hello World file, and decide that we want paragraph alignment and font color to be controlled by the application.

The Page

HTML

To control paragraph alignment for the text in our HTML page, we need to add an 'align' attribute to the <p>element. The <p> element already has an 'id' attribute, so no changes are needed in the HTML for this behavior.

To control font color for the text in our HTML page, we add a <font> element with an 'id' attribute.

DynamicAttribute.html (updated)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p id="DynamicText"><font id="DynamicFont" color="#ff0000">Hello, world!</font>
</body>
</html>

Dynamator File

DynamicAttribute.dyn
<dynamator language="jsp">
  <prolog>
    <%@ page session="false" %>
    <%!
        public
        String
        getGreeting()
        {
            return "Hello World, says Dynamator!";
        }
    
        public
        String
        getTextColor()
        {
            return "#cc00cc";
        }
    
        public
        String
        getParagraphAlignment()
        {
            return "center";
        }
    %>
  </prolog>
  <id name="DynamicText">
    <attr name="align">
        <content>getParagraphAlignment()</content>
    </attr>
  </id>
  <id name="DynamicFont">
    <content>getGreeting()</content>
    <attr name="color">
        <content>getTextColor()</content>
    </attr>
  </id>
</dynamator>

The Dynamator <attr> element identifies the attribute to be modified. It must be placed within a locator element (in this case, the <id> element), which identifies the element containing the attribute to be modified.

In the first case, the 'align' attribute of the 'p' element is given the dynamic value 'getParagraphAlignment()'. Because the 'p' element does not already specify an 'align' attribute, Dynamator will add the attribute.

In the second case, the 'color' attribute of the 'font' element is given the dynamic value 'getTextColor()'. Because the 'font' element already specifies a 'color' attribute, Dynamator will replace the attribute value.

HTML attribute names specified in the Dynamator file must be in lower case.

You may have noticed that the 'content' element has been moved to the 'DynamicFont' id block. In the HTML, the text is subordinate to the 'font' element, and the 'font' element is subordinate to the 'p' element. The 'content' element replaces the entire contents of the element it references. If it had referenced the 'p' element, the 'font' element would have been removed by Dynamator.

JSP file

After processing with Dynamator, the resulting JSP file looks like this:

DynamicAttribute.jsp (generated by Dynamator)

In Action

If you are viewing this page in a servlet engine, you can see the generated page in action.