Listing Article fragments dynamically using Liferay's WCM Templates

 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.

Blogs
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
[...] http://www.liferay.com/web/raymond.auge/blog/-/blogs/advanced-web-content-example-with-ajax... [...] Read More
[...] Bom dia pessoal, estou tendo que criar a parte de noticias no liferay. Estou utilizando atualmente o Web Content para fazer isto. Porem para poder formatar as lista de noticias na "home" do site eu... [...] Read More
This code is not working in liferay 6.1.x version ,so help me out
how to delete record which added by custom structure and template ? is there any way to put delete option on each entry !!! Help Me out