Skip to content

use taglib

Steve Hannah edited this page Jul 13, 2021 · 1 revision

<use-taglib>

Synopsis

Directive indicating that the view should use a specified tag library.

What is a Tag library?

A tag library is essentially a set of import statements that are all bundled together so that they can be included more easily in a view. Some UI kits define several useful UI components spread across many packages, so using the components would require you to add several import statements in each view that uses any of these components.

In such cases the UI kit might define a "tag library" so that all of its components can be imported with a single tag.

Usage

The use-taglib tag can take either a class attribute or a package attribute. The class attribute would specify the class that defines the tag library. The package attribute would include all tag libraries in the given package.

<!-- Load specific tag library -->
<use-taglib class="com.example.mylib.MyTagLibrary"/>

<!-- Load all tag libraries in package -->
<use-taglib package="com.example.mylib"/>

Examples

The following example comes from the Tweetapp demo. Here the <use-tablib> tag is used to import components from the Tweet App UI kit:

<?xml version="1.0"?>
<border view-controller="com.example.tweetapp.controllers.HomePageViewController"
        xsi:noNamespaceSchemaLocation="HomePage.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <title hidden="true"/>

    <use-taglib class="com.codename1.twitterui.TagLibrary"/>
    <import>
        import com.example.tweetapp.providers.NewsFeedProvider;
    </import>

    <collapsibleHeader scrollableComponent="#tweetList">
        <twtTitle>
            <twtsearchButton rad-href="#SearchForm" rad-href-trigger="TWTSearchButton.SEARCH_ACTION"/>
        </twtTitle>
    </collapsibleHeader>
    <sidebar>
        <twtSideBarView/>
    </sidebar>


    <tweetListView
            name="tweetList"
            layout-constraint="center"
            provider="NewsFeedProvider.class"
    />

    <globalTabs layout-constraint="south" selectedTab="com.example.tweetapp.Tweetapp.HOME_TAB"/>

</border>

The <use-taglib class="com.codename1.twitterui.TagLibrary"/> tag makes components such as <twtTitle>, <titleSideBarView>, <tweetListView>, and <globalTabs> available in the namespace.

You can see the definition of the TagLibrary class itself here.

Creating a Tag Library

To create a tag library, you just need to create a Java class with the @TagLibrary annotation. As an example, consider the Tag library defined by the TweetAppUIKit:

The TweetAppUIKit tag library.
package com.codename1.twitterui;

import com.codename1.rad.annotations.TagLib;

/**
 * A tag library for the Twitter UI kit.  Import this into your applications using:
 *
 * [source,xml]
 * ----
 * <use-taglib class="com.codename1.twitterui.TagLibrary"/>
 * ----
 *
 */
@TagLib(imports={
        "import com.codename1.twitterui.builders.*",
        "import com.codename1.twitterui.views.*",
        "import com.codename1.twitterui.models.*",
        "import com.codename1.twitterui.controllers.*",
        "import com.codename1.twitterui.providers.*"

})
public class TagLibrary {
}

If you include this tag library in your view, it will result in the import statements listed in the imports parameter of the @TagLib annotation to be included in the view.

See Also

Clone this wiki locally