RE: RE: How to setup custom macros for use like FTL_Liferay.ftl?

Jamie Sammons, modified 2 Years ago. New Member Posts: 5 Join Date: 6/2/22 Recent Posts

Hello,

Liferay DXP defines several macros in FTL_Liferay.ftl template that you can be used in theme templates to include theme resources, standard portlets, and more. Liferay DXP also exposes its taglibs as FreeMarker macros.

As a Frontend Developer, I would like to create a Macro based component library that can be accessed similar to FTL_Liferay.ftl. Where do I start, in that, where is the Macro library FTL saved, is any JSP required, since the resource-importer is depracated where is the FTL stored to be accessed globaly, most importantly accessed in Fragments.

Thanks & Regards

 

thumbnail
Jamie Sammons, modified 2 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts

I'd avoid all of this nonsense myself. All too often it leads to Freemarker Abuse and translates into poor rendering performance.

Sure it may simplify your development of templates, but the ongoing penalties you pay from it will far exceed the cost.

Instead, look to simple templates doing simple things and, when you have complex logic to apply, use a TemplateContextContributor to inject a java instance that takes care of all of the complexity.

thumbnail
Jamie Sammons, modified 2 Years ago. Liferay Legend Posts: 1555 Join Date: 9/24/09 Recent Posts

It is actually quite simple to do that. Just add the ftl file to the resources folder in a module, e.g.

my-module
 - bnd.bnd
 - build.gradle
 - src/main/resources/mytemplates/MyMacros.ftl
 - <other stuff, java files, ...>

Now you can include your template in other ftl files like this:

<#include "my-module_SERVLET_CONTEXT_/mytemplates/MyMacros.ftl" />

"my-module" is determined from the Bundle-Name or Bundle-SymbolicName from bnd.bnd (I don't recall which one)

Jamie Sammons, modified 2 Years ago. New Member Posts: 5 Join Date: 6/2/22 Recent Posts

Hi Christoph,

Thanks for providing a quick outline, very appreciated.

Regards
Said