telosystools

Open full view…

Sorting collection in template ? velocity tools?

fbockelee
Thu, 27 Feb 2020 16:08:22 GMT

Hi all, I need to sort a collection in velocity. It is possible with velocity tools but they are not included in telosys : by example : --- #foreach ( $attrib in $sorter.sort($entity.attributes,"name")) ${entity.name} ${attrib.name} #end --- Is it possible to add velocity tools without re-compilation of telosys ? Is there a better solution ?

Telosys Team
Fri, 28 Feb 2020 17:46:21 GMT

Hi, Yes it's possible. If you need to use specific Java classes in your templates, you just have to add this classes in the templates bundle folder You can add ".class" files (in "classes" folder) and ".jar" files in ("lib" folder) In your case you will have to add the following "jar" files in the "lib" folder in the template bundles : - `velocity-tools-generic-3.0.jar` ( contains the "SortTool" class ) and also all the dependencies required by "velocity-tools-generic-3.0.jar" - `slf4j-api-1.7.25.jar` - `commons-beanutils-1.8.3.jar` - `commons-logging-1.1.1.jar` ( all the jar files are available on Maven Central ) See this folders example in a Telosys project : [Templates-Lib](//muut.com/u/telosystools/s1/:telosystools:WOM8:templateslib.png.jpg) Once the class is available you can use it as follow in the template : --- ## Step 1 - New instance of the class "SortTool" #set($sorter = $loader.newInstance("org.apache.velocity.tools.generic.SortTool") ) ## Step 2 - Using the class #foreach( $attribute in $sorter.sort($entity.attributes,"name") ) - $attribute.name #end --- See also the "$loader" object documentation : https://www.telosys.org/templates-doc/objects/loader.html

fbockelee
Tue, 03 Mar 2020 10:25:39 GMT

Great ! work fine, thx a lot