Creating Custom Adaptive Tags
The Adaptive Tag Framework allows you to create custom
tags for use in pagelets and gatewayed pages.
The
ATag class is the base class used
to write custom tags
To implement a custom tag, follow the steps
below.
- . To implement a new tag, you must have a tag library.
A tag library is simply a .jar or .dll file with exactly one class
that implements ITagLibraryMetaData.
Java
public static final TagLibraryMetaData LIBRARY = new TagLibraryMetaData
("Sample Tags", "sample", "This library provides sample tags.", 1.0);
.NET
public static readonly TagLibraryMetaData LIBRARY = new TagLibraryMetaData
("Sample Tags", "sample", "This library provides sample tags.", 1.0);
- Create one public static final ITagMetaData member variable that provides the name and description of the tag.
Create a public static final RequiredTagAttribute or OptionalTagAttribute member variable for every
attribute that the tag supports. You can also use standard HTML and
XML attributes; see Accessing Attributes in Custom Adaptive Tags.
Javapublic static final ITagMetaData TAG;
public static final RequiredTagAttribute MESSAGEATTRIBUTE;
public static final OptionalTagAttribute LOCATIONATTRIBUTE;
static
{
TAG = new TagMetaData("hellolocation", "This tag displays a hello message for the given location.");
MESSAGEATTRIBUTE = new RequiredTagAttribute( "message", "The message to display for hellolocation tag", AttributeType.STRING);
LOCATIONATTRIBUTE = new OptionalTagAttribute("location", "The sample location attribute for hellolocation tag", AttributeType.STRING, "World");
}
.NETpublic static readonly ITagMetaData TAG;
public static readonly RequiredTagAttribute MESSAGEATTRIBUTE;
public static readonly OptionalTagAttribute LOCATIONATTRIBUTE;
static HelloLocationTag()
{
TAG = new TagMetaData("hellolocation", "This tag displays a hello message for the given location.");
MESSAGEATTRIBUTE = new RequiredTagAttribute( "message", "The message to display for hellolocation tag", AttributeType.STRING);
LOCATIONATTRIBUTE = new OptionalTagAttribute("location", "The sample location attribute for hellolocation tag", AttributeType.STRING, "World");
}
Type validation is performed by the tag framework automatically.
If an optional attribute is not present in the HTML, the tag framework
will use the default value. In the same code below, the optional attribute
has a default value of "World.".
- Implement the DisplayTag abstract method.
Use this method to create and display HTML. To display any HTML and
tags defined within the tag, call ProcessTagBody and return the resulting HTML. The sample code below adds the "Hello"
string with a user-specified location to an HTMLElement and returns it to be displayed.
Javapublic HTMLElement DisplayTag()
{
String strLocation = GetTagAttributeAsString(LOCATIONATTRIBUTE);
String strMessage = GetTagAttributeAsString(MESSAGEATTRIBUTE);
HTMLElementCollection result = new HTMLElementCollection();
result.AddInnerHTMLString(strMessage + strLocation + "!");
return result;
}
.NETpublic override HTMLElement DisplayTag()
{
String strLocation = GetTagAttributeAsString(LOCATIONATTRIBUTE);
String strMessage = GetTagAttributeAsString(MESSAGEATTRIBUTE);
HTMLElementCollection result = new HTMLElementCollection();
result.ddInnerHTMLString(strMessage + strLocation + "!");
return result;
}
- If the tag should not display any HTML contained within
the tag, use the GetTagType method to return TagType.NO_BODY.
Javapublic TagType GetTagType()
{
return TagType.NO_BODY;
}
.NETpublic override TagType GetTagType()
{
return TagType.NO_BODY;
}
- Implement the Create abstract method to return a new instance
of the tag.
Javapublic ATag Create()
{
return new HelloLocationTag();
}
.NETpublic override ATag Create()
{
return new HelloLocationTag();
}
The ATag class allows you to include a wide range of functionality
in custom tags. For a full list of interfaces and methods, see the
tagdocs. For links to all tagdocs, see
API Libraries. For details on deploying
your custom tag, see
Deploying Custom Adaptive Tags.
- Accessing Browser Session Information in Custom Adaptive TagsTo access browser session information from a custom adaptive tag, use the IEnvironment class.
- Accessing Attributes in Custom Adaptive TagsTo access attributes used in a custom tag, use one of the GetTagAttribute* methods.
- Storing and Accessing Custom Data in Custom Adaptive TagsTo store custom data as member variables using a custom tag, use the SetStateVariable or SetStateSharedVariable methods. To retrieve it, use GetStateVariable or GetStateSharedVariable.
- Including JavaScript in Custom Adaptive TagsTo include JavaScript in a tag, use the AddJavaScript method inside the DisplayTag method.
- Using Nested Tags in Custom Adaptive TagsTags can be used within other tags. To implement nested tags, use the RequiredParentTag, RequiredChildTag and RelatedChildTag member variables.
- Implementing Non-Standard Custom Adaptive Tag TypesTo implement non-standard tag types in custom adaptive tags, including 508-accessible, looping or singleton tags, override the associated method.
- Deploying Custom Adaptive TagsTo deploy custom adaptive tags, follow these steps.