Blogs
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.

