Message Boards

Composing HTML content for serving via REST API response

Jan Tošovský, modified 3 Years ago.

Composing HTML content for serving via REST API response

Liferay Master Posts: 566 Join Date: 7/22/10 Recent Posts
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?
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Composing HTML content for serving via REST API response (Answer)

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
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.
Jan Tošovský, modified 3 Years ago.

RE: Composing HTML content for serving via REST API response

Liferay Master Posts: 566 Join Date: 7/22/10 Recent Posts
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.