In my experience working with Liferay projects, the most used component is Web Content, not only the Basic Web Content. A lot of times the customer needs to create a structure and a template to achieve his goal. In previous Liferay Portal version (aka 6.1) we need to create a hook to create new Asset Display View, but with Liferay Portal 6.2, we can create a simple Application Display Template (ADT) for Asset Publisher and everything is working in a great way and easily.
For this article I created a simple structure with the following fields: Title, SubTitle and Content.
Let's create the ADT to show the Title, SubTitle and a Link to read the full structure.
Solution
The following code access the Web Content Structure in two different ways. Choose which is better to you.
#if (!$entries.isEmpty())
#foreach ($entry in $entries)
#set($renderer = $entry.getAssetRenderer() )
#set($className = $renderer.getClassName() )
#if( $className == "com.liferay.portlet.journal.model.JournalArticle" )
#set( $journalArticle = $renderer.getArticle() )
#set( $document = $saxReaderUtil.read($journalArticle.getContent()) )
#set( $rootElement = $document.getRootElement() )
## first alternative
#foreach( $dynamicElement in $rootElement.elements() )
#if( "subTitle" == $dynamicElement.attributeValue("name") )
#set( $subTitle1 = $dynamicElement.element("dynamic-content").getText() )
first alternative -> $subTitle1 <br />
#end
#end
## second alternative
#set( $xPathSelector = $saxReaderUtil.createXPath("dynamic-element[@name='subTitle']") )
#set( $subTitle2 = $xPathSelector.selectSingleNode($rootElement).getStringValue() )
second alternative -> $subTitle2 <br />
#end
#end
#end
Choose which one is better to you. A improve that you could do for this code is check, if the WebContent (JournalArticle) is the same that the Structure that you are accessing. I didn't to that because I have only one Strucutre.
Let's see the final code.
#if (!$entries.isEmpty())
<div class="news">
#foreach ($entry in $entries)
#set($renderer = $entry.getAssetRenderer() )
#set($className = $renderer.getClassName() )
#if( $className == "com.liferay.portlet.journal.model.JournalArticle" )
#set( $journalArticle = $renderer.getArticle() )
#set( $document = $saxReaderUtil.read($journalArticle.getContent()) )
#set( $rootElement = $document.getRootElement() )
#set( $xPathSelector = $saxReaderUtil.createXPath("dynamic-element[@name='subTitle']") )
#set( $subTitle = $xPathSelector.selectSingleNode($rootElement).getStringValue() )
#set( $link = $renderer.getURLViewInContext($renderRequest, $renderResponse, '') )
<div class="new">
<h1 class="title">$entry.getTitle($locale)</h1>
<h3 class="sub-title">$subTitle</h3>
<p class="read-more">
<a href="$link">Read More</a>
</p>
</div>
#end
#end
</div>
#end
I hope that this code can help you more than is helping me. If you have any trouble, please post in our forum.


