Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can Velocity set a default value for a variable when no value found in VelocityContext?

Tags:

velocity

Velocity just print the tag name if no value was found in VelocityContext, ie, $name in my template file, but there is no value for "name" in VelocityContext, so just "$name" was printed. I want Velocity to print a default value if there is no value for the variable, I just tried to extends AbstractCotnext and override internalGet() method, but the return value of internalGet() will be cast to Node object, I don't know how to create a new Node object in my internalGet() method, and also I think this way is very complex.

is there a simple way to set a default value (default value is just a string) ?

thanks.

like image 268
hiway Avatar asked Dec 14 '12 04:12

hiway


People also ask

How can we set default value to the variable?

You can set the default values for variables by adding ! default flag to the end of the variable value. It will not re-assign the value, if it is already assigned to the variable.

How do you declare a variable in a Velocity template?

Depending on what is added to the Velocity Context (MagicDraw automatically adds its element to the Velocity Context) the variable can either be a local variable or a reference to a Java object. To declare a local variable inside a template, type: $ followed by a string beginning with a letter.

What is the default value of an object that has not been initialized?

In Java, class and instance variables assume a default value (null, 0, false) if they are not initialized manually. However, local variables aren't given a default value. You can declare but not use an uninitialised local variable. In Java, the default value of any object is null.

How does Apache Velocity work?

Velocity separates Java code from the web pages, making the web site more maintainable over the long run and providing a viable alternative to Java Server Pages (JSPs) or PHP. Velocity can be used to generate web pages, SQL, PostScript and other output from templates.


3 Answers

Not easily for all variables as far as I see, I only managed to do it for some variables specifically as follows:

Template:

#if ( !$somevar )
#set ( $somevar = "mycontent" )
#end

Var is: $somevar

Result:

Var is: mycontent
like image 104
centic Avatar answered Oct 25 '22 03:10

centic


Create a velocimacro in your template:

#macro(defaultValue $parm)  
#if (!$!parm || $!parm == "")  
i-like-will
#else  
$parm  
#end  
#end  

And call it like this in the same template:

#defaultValue($name)  

Check Apache Velocity - Velocity User Guide for more info on velocimacros (and velocity in general).

like image 26
Will C. Avatar answered Oct 25 '22 01:10

Will C.


A little late to the party, but you could also perform a check when defining a variable. I had to compress this to one line to remove excess space in the output, but here is an example from one of my projects:

#set ( $qlDefault = "qlinks" )
#set ( $qlClass = "#if($sharedCtaImage.getChild('path').value != '/')$qlDefault#else$qlDefault full#end" )

Default class is defined, then I check if another, specific value is filled in to determine if I keep the default class or append an additional class. This could also work for swapping out classes as well.

like image 41
Sarah L. Avatar answered Oct 25 '22 01:10

Sarah L.