Posts

Showing posts with the label templates

Custom FTL Macro Library in Liferay

Image
Freemarker Macro Library Implementation Overview Liferay has an embedded freemarker library, which contains FTL macroses and functions to help with templates implementation. In this article we’ll review how out-of-the-box FTL Macro library works, and how to implement a custom one.  Liferay FTL Library Review Default liferay FTL library is exposed with a FTL_liferay.ftl file in the portal-template-freemarker module. There is a FreeMarkerEngineConfiguration configuration, which references this library file and defines an alias (“liferay”) for it:  @Meta.AD (   deflt = "FTL_liferay.ftl as liferay" , name = "macro-library" ,   required = false ) public String [] macroLibrary (); This configuration can be modified in System Settings -> Template Engines -> Freemarker Engine: FTL_liferay.ftl file contains a lot of macroses and functions, which can be used in freemarker templates - for themes, widget templates or even fragments....

Registering custom objects for WebContent Templates with TemplateContextContributor

Registering custom objects for WebContent Templates with TemplateContextContributor Sometimes you may need additional data to be available in the web content template. TemplateContextContributor  can help in this case: Create class, that implements  com.liferay.portal.kernel.template.TemplateContextContributor . Add  @Component  annotation to rigister it as OSGi-component. Override  com.lif.app.display.contributor.AppDisplayContributor.prepare(Map<String, Object> contextObjects, HttpServletRequest request)  method and put required object into  contextObjects  map. Deploy module with template context contributor. Use registered context in FTL template for web content. Example: @Component ( immediate = true , property = "type=" + TemplateContextContributor. TYPE_GLOBAL , service = TemplateContextContributor. class ) public class AppDisplayContributor implements TemplateContextContributor { private ...

Accessing Article Information in Web Content Templates

Image
While, it’s pretty simple to access journal article structure fields in template: (you have all these fields listed, and even code sample is generated when you click on it), accessing article information (articleId, author, etc.) is not such straightforward. Solution: To access such information you may use the following syntax: <#assign journalArticleId = . vars [ 'reserved-article-id' ]. data /> This will get the articleId of the article being displayed by the template. In a similar way you can access other fields, sample: <#assign createDate = . vars [ 'reserved-article-create-date' ]. data /> <#assign author = . vars [ 'reserved-article-author-name' ]. data /> All these fields are listed in com.liferay.journal.model.JournalStructureConstants class: public class JournalStructureConstants {    public static final String RESERVED = "reserved" ;    public static final String RESERVED_ARTICLE_ASSET...