Showing posts with label Velocity. Show all posts
Showing posts with label Velocity. Show all posts

Tuesday, February 3, 2015

How to display loop count in velocity

The following syntax will loop the map and display the value in the map.

#foreach($abc in $abcMap)
   $abc.get("a")  $abc.get("b")  $abc.get("c")
#endbbb


To display the loop count, just need to add velocityCount.

#foreach($abc in $abcMap)
   $velocityCount $abc.get("a")  $abc.get("b")  $abc.get("c")
#endbbb



Done!!

Thursday, January 15, 2015

How to display null as empty string with Apache Velocity

We used the following Syntax to display attribute's value in Apache Velocity.
$myValue


But the problem is that, we the value carried by the attribute is null.
Velocity is not displaying an empty string or "null",
it display the attribute name that is being used.

attribute: [ $myValue1, $myValue2, $myValue3, $myValue4 ]
value:      [ "First", null, "Third", "" ]
Output:   [ First, $myValue2, Third,  ]


To display the NULL value as an empty string. we just need to add "!" after "$"

attribute: [ $!myValue1, $!myValue2, $!myValue3, $!myValue4 ]
value:      [ "First", null, "Third", "" ]
Output:   [ First, , Third,  ]


Done!!

Tuesday, January 13, 2015

How to retrieve value in map with Apache Velocity

Assuming we have a HashMap with the following value

HashMap myMap = new HashMap();
myMap.put("k1", "v1");
myMap.put("k2", "v2");
myMap.put("k3", "v3");


put the HashMap into the VelocityContext.

VelocityContext context = new VelocityContext();
context.put("myMap", myMap);


To retrieve the map value in the Velocity template (.vm file),
Use the following syntax to retrieve map value with constant key.

$myMap.get("k1");
$myMap.get("k2");
$myMap.get("k3");


Use the following syntax to retrieve map value with dynamic key.

$myMap.get($mapKey1);
$myMap.get($mapKey2);
$myMap.get($mapKey3);


Done!!

Thursday, November 6, 2014

reading java.util.List in velocity template

Assuming a list pass to the Velocity context.


List list = new ArrayList();
list.add("item1");
list.add("item2");
list.add("item3");

VelocityContext context = new VelocityContext();
context.put("list", list);


To get the list value,
below is the syntax in Velocity template

$list.get(0)

$list.get(1)

$list.get(2)


Done!!

Tuesday, October 28, 2014

Loop index in Apache Velocity

To get the loop index in Velocity template,
we need to initiate a temporary index variable, and increase the variable count by 1 after every occurrence in the loop.

#set( $count = 1 )
<table>
#foreach( $customer in $customers )
    <tr>
        <td>$count</td>
        <td>$customer.name</td>
        <td>$customer.age</td>
    </tr>
#set( $count = $count + 1 )
#end
</table>

the idea is what we have in the for loop.
int count = 1;
for(Customer c : customerList) {
    print(...);
    count++;
}


Done!!

Saturday, February 22, 2014

Velocity without template(.vm) file

Velocity can be used without a .vm file.
The original template string could be stored in other places, eg. database, or coded in java class.

But there are slightly different to merge the template string with the passed in parameters.

Below is the sample code.
// template string coded in java class or retrieve from database
String templateString = "Using $project $name without vm file.";

// initialize Velocity
VelocityContext context = new VelocityContext();
context.put("name", "Velocity");
context.put("project", "Jakarta");

String output = new StringWriter();

// evaluate the template string and merge them together
Velocity.evaluate(context, output, "log or null", templateString);
System.out.println("output: " + output);

***to use Velocity with .vm file, we use template.merge( context, output )
***to use Velocity without .vm file, we use Velocity.evaluate( context, output, logTag, templateString )


Done!!

Monday, February 3, 2014

How to set up Velocity in Liferay?

To use Velocity in Liferay Portlet, we must fulfill the Velocity dependencies.
As Liferay already make sure of Velocity in layouts and themes, thus we just need to import Velocity's required jar into our customized Portlet.

This post is actually referring to Adding liferay libraries to customized portlet classpath and this is the extension of this post.

required jar files to use Velocity are
commons-lang.jar
oro.jar
velocity.jar


Done!!

Monday, January 27, 2014

Loading Velocity template in JSF

When loading Velocity template with the default setting.
The following exception might be hit
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource

Even I used the Class Resource Loader, still hitting the same exception.

The exception solved when used File Resource Loader together with the system Real Path.
String myTemplatePath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/WEB-INF/mytemplatepath");

Properties p = new Properties();
p.setProperty( "file.resource.loader.path", myTemplatePath);
Velocity.init(p);

Template template = Velocity.getTemplate( "mytemplate1.vm" );

p/s: assuming all .vm files are kept under <webapp>/WEB-INF/mytemplatepath folder.

Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...