Liferay have given ADT API to implement ADT support for Out
of Box Portlet that is Custom Portlets.

Generally what are the variables or elements which required
in view logic we will group it and will be stored in template engine and whenever
we need in views we simply use reference names to access the variables or
elements from template engine and all these mechanism was implemented in
Liferay ADT API so that we can implement ADT Support to custom Portlets.

Liferay Have Template Handler which will take care of the
groping variables and store into template engine and at the time of render we
will use reference variable to access data from template engine and these template
engine are velocity or free marker that is why we will use velocity/free marker
templates in the ADT to change Portlet display dynamically.

Implementation ADT
Support to Custom Portlet

Create Liferay MVC
Portlet

Implement Portlet
Display Template Handler Java Class

Register Portlet
Display Template Handler Java Class

Implement ADD_PORTLET_DISPLAY_TEMPLATE
Permission to Custom Portlet

Implement
Configuration View to Portlet

Implement View Logic
to Read Configured Templates

Create Liferay MVC
Portlet

Creating Liferay MVC Portlet is very straight forward step
and you can use Liferay IDE to create Liferay MVC Portlet.

To handle template variables we will use Portlet Display Template
Handler java class from ADT API and it will take care the template variables
which are required in the portlet display and these variables we will defined in the Portlet
Display Template Handler class.

We need to implement Custom Portlet Display Template Handler
class that should extend the BasePortletDisplayTemplateHandler.

BasePortletDisplayTemplateHandler java class consist many
methods so we will override some of the methods in the Custom Portlet Display Template
Handler class

The following are methods

getClassName(): This
will defined what type of entries you are rendering and typically its model class
name(Student.class, Employee.class)

getName():
Declares the name of your ADT type (typically, the name of the portlet).

getResourceName():
Specifies which resource is using the ADT (e.g. a portlet) for permission
checking.

getTemplateVariableGroups(): Defines the
variables exposed in the template editor.

The following is Example Implementation

publicclass ADTPOCPortletDisplayTemplateHandler extends

BasePortletDisplayTemplateHandler {

@Override

public String getClassName() {

// TODO Auto-generated method stub

return Student.class.getName();

}

@Override

public String getName(Locale arg0) {

// TODO Auto-generated method stub

return"ADTPOC Template";

}

@Override

public String getResourceName() {

// TODO Auto-generated method stub

return"ADTPOC_WAR_ADTPOCportlet";

}

public Map<String,
TemplateVariableGroup> getTemplateVariableGroups(

longclassPK, String language, Locale locale)

throws Exception {

Map<String,
TemplateVariableGroup> templateVariableGroups =

super.getTemplateVariableGroups(classPK, language, locale);

TemplateVariableGroup templateVariableGroup =

templateVariableGroups.get("fields");

templateVariableGroup.empty();

templateVariableGroup.addCollectionVariable(

"students", List.class, PortletDisplayTemplateConstants.ENTRIES,

"student", Student.class, "curStudent", "name");

returntemplateVariableGroups;

}

}

Register Portlet
Display Template Handler Java Class

We already implemented Custom Register Portlet Display Template
Handler Java ClassAnd now we need to register this class name so that Portlet
will get ADT Capability to do this we will use Liferay-portlet.xml file
configuration file where we need to configure Custom Portlet Display Template Handler
Java Class and <template-handler/> is tag which should be enclosed by
<portlet/>

Since the ability to add ADTs is new to your portlet, we need
to configure permissions so that administrative users can grant permissions to
the roles that will be allowed to create and manage display templates. Just add
the action key ADD_PORTLET_DISPLAY_TEMPLATE to your portlet’s docroot/WEB-INF/src/resource-actions/default.xml
file

To provide ADT Templet editor to create new ADT templates
will use Liferay Configuration page where we will provide template editor code
so that when we add new templates to Portlet, where you can see all
defined variables.

Now Portlet configuration page(config.jsp) we need to
add following code so that it will provide template editor while design templates
and these can be accessed through Portlet configuration option by administrator.<
liferay-ui:ddm-template-selector/>
is tag which will take care all.

In the example used Dummy Model class Student.java and while
create Student object simply use for loop to create student object and these
will be available as entries in the templates at the time of create new
templates for Portlet display.

After successful deployment you can see the Portlet in
sample category

Login as Admin and Drag and Drop Portlet in the page

The following is Default view of Portlet

Now go to Docbar Admin tab and click on Site Configuration

Configuration section you can see Application Display
Templates click on it

Now you can Add new ADT for our Custom Portlet by Select
Custom Portlet from Add Button Icon and Select Our Custom Portlet

Once we click on Add then it will launch Template Editor
there we need to design New Display to Custom Portlet By using Free
Marker/Velocity Templates. Once you design view then click on save then new ADT
Template will be created for Custom Portlet

Now go to Portlet Configuration

In Setup tab you can see Display Template Drop down there
you can select our newly created Custom Portlet ADT then save it.

Once you apply new ADT then Portlet Display will be changed
such way you can create many Displays to custom Portlet using ADT.