Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Ray Augé
Freemarker & JSP Tags
May 20, 2011 8:43 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1195

Join Date: February 7, 2005

Recent Posts

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:
1<#assign portlet = [b]PortalJspTagLibs[/b]["/WEB-INF/tld/liferay-portlet.tld"] />

If the taglib belongs to the theme (bundled with the theme plugin)
1<#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:
1<#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):
1<@[b]liferay_ui["message"][/b] key="message" />

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

Handling tag attributes
You can only quote string literals
1<@liferay_ui["message"] [b]key="message"[/b] />

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

What about complex assignments then? I suggest another convention, use parentheses around complex/object assignments.
1<@liferay_ui["search-container-column-text"]
2    href=(rowURL)
3    name="my-column"
4    value=[b]("something" + 15 + "something else")[/b]
5/>

Note that freemarker provides "builtins" for conversions so that for instance you can assign numbers to string attributes:
1<@liferay_ui["search-container-column-text"]
2    href=rowURL
3    name="posts"
4    value=(categoryDisplay.getSubcategoriesMessagesCount(curCategory)[b]?string[/b])
5/>

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
1<#assign ParamUtil = [b]staticUtil[/b]["com.liferay.portal.kernel.util.ParamUtil"] />

After that, usage is as simple as:
1<#assign portletResource = ParamUtil.getString(request, "portletResource") />

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


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!
Attachments: life-overflow-theme-6.1.0.1.war (1,361.5k)
Erik Andersson
RE: Freemarker & JSP Tags
June 16, 2011 3:45 PM
Answer

Erik Andersson

Rank: Junior Member

Posts: 39

Join Date: April 8, 2008

Recent Posts

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
Mika Koivisto
RE: Freemarker & JSP Tags
June 16, 2011 3:53 PM
Answer

Mika Koivisto

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1513

Join Date: August 7, 2006

Recent Posts

Hey Erik,

You can't use taglibs in web content templates because they don't have access to the real HttpServletRequest.
Erik Andersson
RE: Freemarker & JSP Tags
June 16, 2011 3:55 PM
Answer

Erik Andersson

Rank: Junior Member

Posts: 39

Join Date: April 8, 2008

Recent Posts

Thanks Mika. Too bad though, I would've loved to be able to do that.
Hari babu
RE: Freemarker & JSP Tags
September 10, 2013 2:55 AM
Answer

Hari babu

Rank: New Member

Posts: 4

Join Date: September 3, 2013

Recent Posts

Hi Mika

You have the request available to the velocity template by un-check the "Cachebale" checkbox on CMS templates
Mika Koivisto
RE: Freemarker & JSP Tags
September 10, 2013 1:03 PM
Answer

Mika Koivisto

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1513

Join Date: August 7, 2006

Recent Posts

Hari it's not a real HttpServletRequest