Prerequisite:
To follow this article you should have some basic knowledge on
i) Liferay Asset Publisher Portlet
ii) Understanding of Application Display Template (ADT)
iii) Velocity Template or Free Marker Template
For your information:
By default Liferay Asset Publisher Portlet can be configured to display below Assets
Iterate All asset Entries
#if (!$entries.isEmpty()) #foreach ($curEntry in $entries) $curEntry.getTitle($locale) #end #end
Display read more link
#if (!$entries.isEmpty()) #foreach ($curEntry in $entries) $curEntry.getTitle($locale) <a href="$assetPublisherHelper.getAssetViewURL($renderRequest, $renderResponse, $curEntry, true)">...readMore</a> #end #end
Get user related information
##The object $user is ready to use in template First Name:$user.getFirstName(); Last Name:$user.getLastName(); Full Name:$user.getFullName();
Date utility
##Get current Date in dd-MM-yyyy format $dateUtil.getCurrentDate("dd-MM-yyyy", $locale) ## Format date object where $lastLoginDate is date object ## The date object is formatted to dd MMM yyyy format #set ($lastLoginDate = $user.getLastLoginDate()) $dateUtil.getDate($lastLoginDate, "dd MMM yyyy", $locale) ##Convert String to date object $dateUtil.parseDate("yyyy-MM-dd","some-date-string", $locale)
Using $locale object
##Get key value from resource bundle. Here asset-services is the key $languageUtil.get($locale, "asset-services") ##Get Current Language Id $locale.toString() ##Compare current Language Id with another. ##Use the below in if condition $locale.toString().equals("en_US")
Identifying Asset Type
##Identify Journal Article #if (!$entries.isEmpty()) #foreach ($curEntry in $entries) #set($renderer = $curEntry.getAssetRenderer() ) #set($className = $renderer.getClassName() ) #if( $className == "com.liferay.journal.model.JournalArticle" ) ## Some other logic goes here #end #end #end
In the above source code the line if( $className == “com.liferay.journal.model.JournalArticle” ) identifies the Journal Article. Similarly we can compare the below default Asset Types
com.liferay.wiki.model.WikiPage
com.liferay.document.library.kernel.model.DLFolder
com.liferay.document.library.kernel.model.DLFileEntry
com.liferay.blogs.kernel.model.BlogsEntry
com.liferay.bookmarks.model.BookmarksFolder
com.liferay.bookmarks.model.BookmarksEntry
com.liferay.message.boards.kernel.model.MBMessage
com.liferay.dynamic.data.lists.model.DDLRecord
com.liferay.calendar.model.CalendarBooking
com.liferay.journal.model.JournalFolder
Iterate Journal Article Structure Data
#if (!$entries.isEmpty()) #foreach ($curEntry in $entries) #set($renderer = $curEntry.getAssetRenderer() ) #set($className = $renderer.getClassName() ) #if( $className == "com.liferay.journal.model.JournalArticle" ) #set( $journalArticle = $renderer.getArticle() ) #set( $document = $saxReaderUtil.read($journalArticle.getContent()) ) #set( $rootElement = $document.getRootElement() ) #foreach( $dynamicElement in $rootElement.elements() ) ##Say that in Web Content Structure there is a field name newsLocation #if( "newsLocation" == $dynamicElement.attributeValue("name") ) ##Retrieve the data from newsLocation field #set( $newsLocation = $dynamicElement.element("dynamic-content").getText()) #end #end #end #end #end
Miscellaneous
## Get Company ID $themeDisplay.getCompanyId() ## Get Scope Group ID $scopeGroupId ##Get Current plId $plid ##Get Current Page Title $themeDisplay.getLayout().getName($locale) ##Iterate all the current menu items (i.e., navigation items) #foreach( $item in $navItems ) $item.getName() #end
Summery
All the above codes are velocity code and has been tested in Liferay 7. If you need more code snippets related to Asset Publisher ADT then write your comment in the below.