留言板

Anyone Have Velocity Tools Working in Liferay 6?

Brent Williams,修改在12 年前。

Anyone Have Velocity Tools Working in Liferay 6?

New Member 帖子: 8 加入日期: 11-10-18 最近的帖子
Hi All,

I am trying to get Velocity Tools 2.0 working in Liferay 6 so I can access object such as the SortTool within my velocity templates, but just can't get it to work. Documentation is very sparse, but from what I have seen I can drop the velocity-tools-2.0.jar in the /lib directory of my server and place tools.xml in the web-inf directory and should be good to go, however whenever I try to access something basic such as:

$convert.toInteger('12.6')

it just prints the code and does not parse it. Anyone know where to get step-by-step instructions for deploying this with Liferay?

Thanks!,
Brent
thumbnail
Samir Gami,修改在12 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

Regular Member 帖子: 162 加入日期: 11-2-4 最近的帖子
Brent, From where you are trying to access sortTool, Are you working with Theme or portlet ??
Brent Williams,修改在12 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

New Member 帖子: 8 加入日期: 11-10-18 最近的帖子
I'm working in the Velocity template associated to the structure I'm using inside the web content portlet.
Brent Williams,修改在12 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

New Member 帖子: 8 加入日期: 11-10-18 最近的帖子
Ok, we got it solved.....

First of all, we were looking at exactly how the $sortTool.sort() method works, thinking the foreach loop actually did the sorting when it actually returns a collection that you are iterating through:

#foreach ($obj in $sortTool.sort($objects, "fieldnametosorton")
Display data from the sorted list here like $obj.fieldname
#end

Secondly, since we were sorting on a child field (reference previous post) we had to first build an array of maps and then sort on that since the sort method can't seem to access the child field:

#set ($array = [])
#foreach ($obj in $objects)
#set ($void = $array.add( { "field1" : $obj.getData(), "childfield" : $obj.childfield.getData() } ) )
#end

Then you can sort properly:

#foreach ($obj in $sortTool.sort($array, "childfield")
<p>This is a line sorted on $obj.childfield</p>
#end

Hopefully that helps someone!
thumbnail
sunil G,修改在11 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

New Member 帖子: 6 加入日期: 11-12-9 最近的帖子
Hi Brent,

Is there any quick guide to sort tool in liferay.

Thanks in advance,
sunil
thumbnail
Nikhil Nishchal,修改在11 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

Regular Member 帖子: 177 加入日期: 12-6-22 最近的帖子
Hi,
Velocity markup provides $sortTool to sort any collection.
Example :
#set ($menuItems= [])
#set ($tmp = $menuItems.add({"title" : "item1", "url" : "http://www.google.com"}))
#set ($tmp = $menuItems.add({"title" : "item2", "url" : "http://www.google.com"}))
#foreach ($item in $sortTool.sort($menuItems, 'title:asc'))
<a href="$item.url">$item.title</a><br>
#end
#foreach ($item in $sortTool.sort($menuItems, 'title:desc'))
<a href="$item.url">$item.title</a><br>
#end

Reference:
http://www.liferay.com/community/forums/-/message_boards/message/6162176
sortTool doc - http://velocity.apache.org/tools/releases/1.4/javadoc/org/apache/velocity/tools/generic/SortTool.html
thumbnail
sunil G,修改在11 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

New Member 帖子: 6 加入日期: 11-12-9 最近的帖子
Thx nikil, it was really helpful..
thumbnail
Nikhil Nishchal,修改在11 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

Regular Member 帖子: 177 加入日期: 12-6-22 最近的帖子
sunil G:
Thx nikil, it was really helpful..



You are welcome emoticon
thumbnail
Amos Fong,修改在12 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

Liferay Legend 帖子: 2047 加入日期: 08-10-7 最近的帖子
Brent,

Some of the tools should already be available, see http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Journal+Template+(Velocity) (I think the tools are version 1.6 though)

try:

$sortTool

Adding more custom variables is as simple as dropping the jar and xml in. See this wiki:
http://www.liferay.com/community/wiki/-/wiki/Main/Custom+Velocity+Variables

btw, I don't see convert as a tool, try this (http://velocity.apache.org/tools/devel/generic/MathTool.html:
$mathTool.toInteger("12.6")
Brent Williams,修改在12 年前。

RE: Anyone Have Velocity Tools Working in Liferay 6?

New Member 帖子: 8 加入日期: 11-10-18 最近的帖子
I'm accessing sortTool in the Velocity Template for the generic web content portlet in liferay. Thank you the mathTool works and sortTool is iterating through my objects list but is not working. I'm following the docs here:

http://www.docjar.org/docs/api/org/apache/velocity/tools/generic/SortTool.html

but all that happens is the data is printed to the screen inside the foreach loop, so I'm not sure how to actually implement the sortTool and can't seem to find any actual examples.

BTW, my structure looks like this:

headline (text field, repeatable)
-- body (HTML text box) (child element)
-- order (Text field) (child element)

I'm grabbing the main content record and all of its siblings and calling the sort method.

The headline.order field is what I'm trying to sort on.

Thanks,
Brent