Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Composing HTML content for serving via REST API response
I'd like to serve my entities as prerendered HTML chunks via REST API. I'd like to avoid dumb string concatenation or DOM manipulation. I'd prefer utilizing some template language and injecting variables into the structure. Then compile the result and serve it as HTML. I am aware of FreeMarker templates used besides JSP for rendering some content, however, it is not clear if this can be somehow utilized for this use case.
Any idea?
Any idea?
Sure, why not? To implement this yourself you would have to do the same as the freemarker portlet in the include method:
https://github.com/liferay/liferay-portal/blob/467222dbc7a4d5eab79465a735db17d695b0e27f/util-bridges/src/com/liferay/util/bridges/freemarker/FreeMarkerPortlet.java
We usually just return a json object from our rest calls and do whatever rendering is necessary in the javascript frontend. But there is no reason this shouldn't work.
https://github.com/liferay/liferay-portal/blob/467222dbc7a4d5eab79465a735db17d695b0e27f/util-bridges/src/com/liferay/util/bridges/freemarker/FreeMarkerPortlet.java
We usually just return a json object from our rest calls and do whatever rendering is necessary in the javascript frontend. But there is no reason this shouldn't work.
Thanks a lot for that link. I've just made it to work. The most problematic was composing that "resourcePath" pointing to my FTL template. A special syntax is expected here so it has to be constructed as classLoaderName + TemplateConstants.CLASS_LOADER_SEPARATOR + RESOURCE_PATH, i.e. the final string is something like this: my.domain.portal.myportlet_1.0.0_CLASS_LOADER_CONTEXT_/META-INF/resources/template.ftl.
Currently I have that classLoaderName hardcoded as I couldn't find any way to get it dynamically.
Currently I have that classLoaderName hardcoded as I couldn't find any way to get it dynamically.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™