Implementing Widget Templates

Widget Templates are ways to customize how a widget looks. You can create templates for a widget’s display and then choose which template is active.

Figure 1: By using a custom display template, your portlets display can be customized.

Figure 1: By using a custom display template, your portlet's display can be customized.

To add Widget Template support to your portlet, follow the steps below.

  1. Create and register a custom *PortletDisplayTemplateHandler component. Liferay provides the BasePortletDisplayTemplateHandler as a base implementation for you to extend. You can check the TemplateHandler interface Javadoc to learn about each template handler method.

    The @Component annotation ties your handler to a specific portlet by setting the property to your portlet’s name. The same property should be found in your portlet class. For example,

        immediate = true,
        property = {
            ""+ AssetCategoriesNavigationPortletKeys.ASSET_CATEGORIES_NAVIGATION
        service = TemplateHandler.class

    The Site Map widget sets the @Component annotation like this:

        immediate = true,
        property = "" + SiteNavigationSiteMapPortletKeys.SITE_NAVIGATION_SITE_MAP,
        service = TemplateHandler.class
    public class SiteNavigationSiteMapPortletDisplayTemplateHandler
        extends BasePortletDisplayTemplateHandler {

    You’ll continue stepping through the Site map widget’s TemplateHandler implementation next.

  2. Override the base class’ getClassName(), getName(...), and getResourceName() methods:

    public String getClassName() {
        return LayoutSet.class.getName();
    public String getName(Locale locale) {
        String portletTitle = _portal.getPortletTitle(
            ResourceBundleUtil.getBundle(locale, getClass()));
        return LanguageUtil.format(locale, "x-template", portletTitle, false);
    public String getResourceName() {
        return SiteNavigationSiteMapPortletKeys.SITE_NAVIGATION_SITE_MAP;

    These methods return the template handler’s class name, the template handler’s name (via resource bundle), and the resource name associated with the Widget Template, respectively.

  3. Override the getTemplateVariableGroups(...) method to return your widget template’s script variable groups. These are used to display hints in the template editor palette.

    public Map<String, TemplateVariableGroup> getTemplateVariableGroups(
            long classPK, String language, Locale locale)
        throws Exception {
        Map<String, TemplateVariableGroup> templateVariableGroups =
            super.getTemplateVariableGroups(classPK, language, locale);
        TemplateVariableGroup templateVariableGroup =
            "pages", List.class, PortletDisplayTemplateConstants.ENTRIES,
            "page", Layout.class, "curPage", "getName(locale)");
            SiteNavigationSiteMapDisplayContext.class, "siteMapDisplayContext");
        return templateVariableGroups;

    For this example, the Pages and Site Map Display Context fields are added to the default variables in the template editor palette.

    Figure 2: You can click a variable to add it to the template editor.

    Figure 2: You can click a variable to add it to the template editor.

  4. Set your display template configuration file path:

    protected String getTemplatesConfigPath() {
        return "com/liferay/site/navigation/site/map/web/portlet/template" +

    This method returns the XML file containing the display template definitions available for your portlet. You’ll create this file next.

  5. Create your portlet-display-templates.xml file to define your display template definitions. For example,

    <?xml version="1.0"?>

    This defined template option is read and presented to the user through the widget’s Configuration menu. Navigate to the Site Map widget’s Configuration menu and you can confirm the Multi Column Layout option is available.

    Figure 3: You can choose the Widget Template you want to apply from the widgets Configuration menu.

    Figure 3: You can choose the Widget Template you want to apply from the widget's Configuration menu.

    This template is created using FreeMarker. You’ll create this template option next.

  6. Create your template script file that you specified in the previous step. For the Site Map widget, its Multi Column Layout option is configured in a FreeMarker template like this:

    <#if entries?has_content>
            <#list entries as entry>
                <#if layoutPermission.containsWithoutViewableGroup(permissionChecker, entry, "VIEW")>
                    <@liferay_aui.col width=25>
                        <div class="results-header">
                                <#assign layoutType = entry.getLayoutType() />
                                <#if layoutType.isBrowsable()>
                                    href="${portalUtil.getLayoutURL(entry, themeDisplay)}"
    <#macro displayPages
        <#if pages?has_content && ((depth < displayDepth?number) || (displayDepth?number == 0))>
            <ul class="child-pages">
                <#list pages as page>
                        <#assign pageType = page.getLayoutType() />
                        <#if pageType.isBrowsable()>
                            href="${portalUtil.getLayoutURL(page, themeDisplay)}"
                            depth=depth + 1

    This template definition enforces page permissions, formats how the pages are displayed (multi column), and provides clickable links for each page.

  7. Your widget must define permissions for creating and managing display templates. Add the action key ADD_PORTLET_DISPLAY_TEMPLATE to your portlet’s /src/main/resources/resource-actions/default.xml file:

    <?xml version="1.0"?>
    <!DOCTYPE resource-action-mapping PUBLIC "-//Liferay//DTD Resource Action Mapping 7.2.0//EN" "">
  8. If your widget hasn’t defined Liferay permissions before, create a file named in the /resources folder and add the following contents providing the path to your default.xml:
  9. Now expose the Widget Template selector to your users. Include the <liferay-ddm:template-selector> tag in the JSP file you’re using to control your portlet’s configuration.

    For example, it may be helpful for you to insert a <liferay-frontend:fieldset> in your configuration JSP file like this:

        collapsible="<%= true %>"
        <div class="display-template">
                classNameId="<%= YourEntity.class.getName() %>"
                displayStyle="<%= displayStyle %>"
                displayStyleGroupId="<%= displayStyleGroupId %>"
                refreshURL="<%= PortalUtil.getCurrentURL(request) %>"
                showEmptyOption="<%= true %>"

    In this JSP, the <liferay-ddm:template-selector> tag specifies the Display Template drop-down menu to be used in the widget’s Configuration menu.

  10. You must now extend your view code to render your portlet using the selected Widget Template.

    First, initialize the Java variables needed for the Widget Template:

    String displayStyle = GetterUtil.getString(portletPreferences.getValue("displayStyle", StringPool.BLANK));
    long displayStyleGroupId = GetterUtil.getLong(portletPreferences.getValue("displayStyleGroupId", null), scopeGroupId);

    Next, you can test if the Widget Template is configured, grab the entities to be rendered, and render them using the Widget Template. The tag <liferay-ddm:template-renderer> aids with this process. It automatically uses the selected template or renders its body if no template is selected.

    Here’s some example code that demonstrates implementing this:

        className="<%= YourEntity.class.getName() %>"
        contextObjects="<%= contextObjects %>"
        displayStyle="<%= displayStyle %>"
        displayStyleGroupId="<%= displayStyleGroupId %>"
        entries="<%= yourEntities %>"
        <%-- The code that renders the default view should be inserted here. --%>

    In this step, you initialized variables dealing with the display settings (displayStyle and displayStyleGroupId) and passed them to the tag along with other parameters.

    As an example, the Site Map widget implements the <liferay-ddm:template-renderer> tag in its view.jsp like this:

        className="<%= LayoutSet.class.getName() %>"
        contextObjects="<%= contextObjects %>"
        displayStyle="<%= siteNavigationSiteMapPortletInstanceConfiguration.displayStyle() %>"
        displayStyleGroupId="<%= siteNavigationSiteMapDisplayContext.getDisplayStyleGroupId() %>"
        entries="<%= siteNavigationSiteMapDisplayContext.getRootLayouts() %>"
        <%= siteNavigationSiteMapDisplayContext.buildSiteMap() %>

    This logic builds the site’s navigation map when the widget is added to a page.

Awesome! Your portlet now supports Widget Templates! Once your script is uploaded and saved, Users with the specified Roles can select the template when they’re configuring the display settings of your portlet on a page. You can visit the Styling Widgets with Widget Templates section for more details on using Widget Templates.

« Introduction to Customizing WidgetsIntroduction to Dynamic Includes »