Skip to content

1.1 Essentials

In the following you will find general explanations on how to use velocity script expressions.

1.1.1 Comments

Comments in scripts are marked with "##":

## Comment: This text is not executed.

1.1.2 Variable assignments

Variable assignments are made by:

## Fixed String Value
#set($variableName=" Value as string ")
## Fixed Integer Value
#set($variableName=1)
## Value from another variable
#set($variableName=$othervariableName)
## Result value from a function call
#set($emailIsValid=$ScriptHelper.validateEmail($!requestParamete rs.EMAIL))

1.1.3 Outputting variables in forms

The variables that are determined and assigned in a script can be output in the HTML texts of an E-Marketing Manager form.

Attention: This is not the script text, but the text for the success or error page of the form.

<html>
The variable has the value: $variableName
</html>

1.1.4 Use of variables in surrounding text

If the use of a variable with surrounding text becomes ambiguous, the variable name can also be enclosed with curly braces at any time:

The price is: ${variableName}Euro

1.1.5 Generating an empty list

Velocity scripts can also be used to create an empty list, which is then filled and reused.

Direct constructor calls are not possible in velocity scripts.

#set($list=$ScriptHelper.newLinkedList())
$list.add("some value 1")
$list.add("some value 2")
</code
==== Creation and use of a map ====
A map / dictionary / table can be created and then filled with values as follows:
<code>
## Create new map
#set($mapVariable={})
## Fixed integer value as new value
#set($mapVariable["new Key"]=1)
## oder
$mapVariable.put("new Key", 2)
## Fixed text Value as new value
#set($mapVariable["new Key"]="Abc123")
## oder
$mapVariable.put("new Key", "Abc124")

1.1.6 Branches (IF)

Velocity scripts can be used to generate program code with branches.

#set($customerID = 1234)
#if($customerID != 0)
## Recipient was found
#else
## Recipient was not found
#end

1.1.7 Grinding (While, Foreach)

True While loops do not exist in Velocity.

Foreach loops look like this:

#set($allMailingLists = [1234, 1235, 1236])
#foreach($mailingList in $allMailingLists)
## Code to be executed for each of the MailiniglistIDs
#end

1.1.8 Direct use of the request parameters and request header

The exclamation mark means that an empty value is returned here, even if the parameter does not exist in the request at all. Without an exclamation mark, an error would be thrown and the script would be aborted.

$!requestParameters.email ## Caseinsensitive specification of the parameter name "Email" is allowed
#set($referer="Request was sent without referer") ## default 
#set($!referer=$_request.getHeader("referer")) 
## Overwrites the default value in "$referer" only if there is a new value.

1.1.9 String editing (Substring)

Due to a bug in the velocity it was not possible to create the corresponding function by "length()-1" in the substring, therefore "length()+-1“

#set($text = "Abcd12345")
#set($newLength = $text.length()+-1)
#set($textNew = $text.substring(0, $newLength))
## => $textNew = "Abcd1234"

1.1.10 Calculating with numerical values from strings

Numerical values from request parameters are always strings, but no arithmetic operations can be performed on them. Therefore, these must first be converted into numbers. These must, however, be converted back into strings for display in HTML texts.

#set($numberInString="12345")
#set($numberToCalculate =
$ScriptHelper.parseInt($numberInString))
#set($numberToCalculate = $zahlZumRechnen + 1)
#set($numberAgainAsString = " $numberToCalculate ") ## Important are the spaces in the quotation marks

1.1.11 Display of the success page or error page for forms

An E-Marketing Manager velocity script can (mostly at the end) set a certain string value (NOT integer) in the variable "scriptResult", which determines whether the success page or the error page should be displayed at the end of the script call within a form.

Display the success page: #set($scriptResult="1")

Error page display:

#set($scriptResult="0")