Liferay DXP 2026.Q4    |    Release Webinar: 18 November 2026    |    Replay coming soon! 

Blogs

Listing Article fragments dynamically using Liferay's WCM Templates

Ray Augé
Ray Augé
Lesen in unter einer Minute

 So my last blog post involved a classloader trick for velocity but it also involved a problem of dynamically listing article fragments on a page.

Now, there are a few ways to do this, but the two that come to mind are:

1) Render each article using a specified template which only shows the content of the desired fields (fine, but seems overkill)

2) Parse the article content XML to get only specified bits (also fine, but there are concerns when the content is localized, not to mention isn't the XML handling code ugly/heavy?)

I've mostly taken approach 1), but could approach 2) be clean and light if we do it right? I think it can!

Here is how I did it (using the trick from the last post of course ):

#set ($journalArticleLocalService = $serviceLocator.findService('com.liferay.portlet.journal.service.JournalArticleLocalService'))
#set ($localeTransformer = $portal.getClass().forName('com.liferay.portlet.journal.util.LocaleTransformerListener').newInstance())
#set ($VOID = $localeTransformer.setLanguageId($request.theme-display.language-id))

#set ($companyId = $getterUtil.getLong($request.theme-display.company-id))
#set ($scopeGroupId = $getterUtil.getLong($request.theme-display.scope-group-id))

#set ($obc = $portal.getClass().forName("com.liferay.portlet.journal.util.comparator.ArticleDisplayDateComparator").newInstance())

#set ($articles = $journalArticleLocalService.search($companyId, $scopeGroupId, '', null, null, null, null, null, null, 0, null, -1, -1, $obc))

<ul> 
#foreach ($article in $articles)
     #set ($xml = $localeTransformer.onXml($article.content))
     #set ($doc = $saxReaderUtil.read($xml))
     #set ($title = $doc.valueOf("//dynamic-element[@name='title']/dynamic-content/text()"))
     <li>${title}</li>
#end
</ul>

 

Now that's contrived, but it's pretty lean, mean and straight to the point, which is always good.

Seitenkommentare
hello ray, I had a similar problem and been helped by your article, but after I tried the code you gave, I get double the value of dynamic element after a few times to add web content, what happens?

Do you have a way to get a value that does not double again?, distinct or something?

thanks again

Related Assets...

Keine Ergebnisse gefunden

More Blog Entries...

Himanshu Chauhan
November 20, 2026
Tony Lim
November 19, 2026