Foren

Freemarker & JSP Tags

thumbnail
Ray Augé, geändert vor 12 Jahren.

Freemarker & JSP Tags

Liferay Legend Beiträge: 1197 Beitrittsdatum: 08.02.05 Neueste Beiträge
Hey All,

At ECS Greg Amerson and I did a workshop to show how, using freemarker & it's JSP Tags support, to overload portal/plugin jsps from your theme.

I can't say it was easy to achieve the understanding to do get everything working, but we finally did and I just wanted to highlight the caveats that caused some pain (and hopefully from here it'll make it easier for everyone else).

So, without further ado, here they are:

*Freemarker doesn't accept '-' (dash) in identifiers* including taglib or tag names (more on this later).

Initializing a taglib for use
If the taglib belongs to the portal:
<#assign portlet = [b]PortalJspTagLibs[/b]["/WEB-INF/tld/liferay-portlet.tld"] />

If the taglib belongs to the theme (bundled with the theme plugin)
<#assign taglib = [b]ThemeJspTaglibs[/b]["/WEB-INF/tld/taglib.tld"] />

If the taglib contains a '-' (dash) in it's name, I would suggest a convention like:
<#assign [b]liferay_ui[/b] = PortalJspTagLibs["/WEB-INF/tld/liferay-ui.tld"] />

Using JSP Tags
The syntax is strange and foreign but I guess we have to live with it. There are two syntax but in the Liferay tradition I will only show and suggest the one that works for all cases. I repeat the fact that freemarker doesn't work with tags that contain dashes in their name. This means we have to use the "map" syntax to call them. It also means we have to use the "short" closing tag syntax as well (not officially, but I couldn't get it to work consistently using the long form):
<@[b]liferay_ui["message"][/b] key="message" />

And the short closing tag syntax
<@liferay_ui["panel-container"] cssClass="message-boards-panels">
		...
[b]<!--@-->[/b]

Handling tag attributes
You can only quote string literals
&lt;@liferay_ui["message"] [b]key="message"[/b] /&gt;

When assigning objects (and remember there are no primitive types in template languages usually) you don't quote them
&lt;@liferay_ui["message"] [b]key=myKeyVar[/b] /&gt;

What about complex assignments then? I suggest another convention, use parentheses around complex/object assignments.
&lt;@liferay_ui["search-container-column-text"]
	href=(rowURL)
	name="my-column"
	value=[b]("something" + 15 + "something else")[/b]
/&gt;

Note that freemarker provides "builtins" for conversions so that for instance you can assign numbers to string attributes:
&lt;@liferay_ui["search-container-column-text"]
	href=rowURL
	name="posts"
	value=(categoryDisplay.getSubcategoriesMessagesCount(curCategory)[b]?string[/b])
/&gt;

Accessing Static classes' methods and/or fields
You have to jump through a hoop here and leverage a utility of freemarker to wrap the static class so that it exposes the methods and/or fields
&lt;#assign ParamUtil = [b]staticUtil[/b]["com.liferay.portal.kernel.util.ParamUtil"] /&gt;

After that, usage is as simple as:
&lt;#assign portletResource = ParamUtil.getString(request, "portletResource") /&gt;

Initializing an instance of a class
Here you have to also leverage a freemarker util or wrapper
&lt;#assign rssURLParams = objectUtil("com.liferay.portal.kernel.util.StringBundler") /&gt;


Finally, I've attached a sample similar to what I used at ECS-2011 (with a few changes to match above recommendations).

The big points here is that you can achieve per theme UI changes so that you can apply those to only portions of your site(s), doing things like using AUI tags in your themes. You could make the same APP have completely different UIs in different sites without touching the core. You can even have this apply technique to jsps in plugin portlets.

Note: In order to see this in action you first have to apply it to a page or site, and then make a Message Board Category of type "Question". Once you navigate into this category you will see the message "this-is-the-overloaded-view" just under the Category name.

Hope it helps!
thumbnail
Erik Andersson, geändert vor 12 Jahren.

RE: Freemarker & JSP Tags

Junior Member Beiträge: 39 Beitrittsdatum: 08.04.08 Neueste Beiträge
Hi Ray,

Thanks for this. I'm trying to get the liferay taglibs to work in web content templates (ftl) by adding:
<#assign aui = PortalJspTagLibs["/WEB-INF/tld/liferay-aui.tld"] />

This doesn't seem to work, however, the console prints:

Expression PortalJspTagLibs is undefined on line 1, column 16 in 2028911937.
The problematic instruction:
----------
==> assignment: aui=PortalJspTagLibs["/WEB-INF/tld/liferay-aui.tld"] [on line 1, column 1 in 2028911937]



Are you able to use the taglibs in web content templates?

Cheers,
Erik
thumbnail
Mika Koivisto, geändert vor 12 Jahren.

RE: Freemarker & JSP Tags

Liferay Legend Beiträge: 1519 Beitrittsdatum: 07.08.06 Neueste Beiträge
Hey Erik,

You can't use taglibs in web content templates because they don't have access to the real HttpServletRequest.
thumbnail
Erik Andersson, geändert vor 12 Jahren.

RE: Freemarker & JSP Tags

Junior Member Beiträge: 39 Beitrittsdatum: 08.04.08 Neueste Beiträge
Thanks Mika. Too bad though, I would've loved to be able to do that.
Hari babu, geändert vor 10 Jahren.

RE: Freemarker & JSP Tags

New Member Beiträge: 4 Beitrittsdatum: 04.09.13 Neueste Beiträge
Hi Mika

You have the request available to the velocity template by un-check the "Cachebale" checkbox on CMS templates
thumbnail
Mika Koivisto, geändert vor 10 Jahren.

RE: Freemarker & JSP Tags

Liferay Legend Beiträge: 1519 Beitrittsdatum: 07.08.06 Neueste Beiträge
Hari it's not a real HttpServletRequest