Script

The Script portlet has two parts - JavaScript and Velocity. All global variables declared in JavaScript wil be propagated so they are available in the Velocity part for easy rendering.

Elements available in the JavaScript context

Name
Type and Description

request

javax.portlet.PortletRequest


The portlet request

out

java.io.PrintWriter

 

The output writer, use for output if you don't render your output in the Velocity part

 

Elements available in the Velocity context

Name
Type and Description

request

javax.portlet.PortletRequest

 

The portlet request

< any >

< any >

 

An Object/value that was declared as a variable in the JavaScript part

The JavaScript (and Velocity) is of course executed server-side!
(what script engine to use can be selected in engine setting dropdown)

Variable propagation

Global variables declared in the JavaScript part are accessible in the Velocity part. An example that uses the SiteVision Public API is shown below:

Javascript

Four variables are declared in the JavaScript part above so there are five (request + 4) references available on the Velocity Context:

$request
$propertyUtil
$portletContextUtil
$user
$name
Velocity

Handling native JavaScript objects in Velocity

Complex native JavaScript objects can almost always be handled by Velocity as they typically can be treated as maps or lists. An example: If your JavaScript variable looks like this:

var company = {
   name: 'SiteVision AB',   
   year: 2002,
   employees: ['Magnus', 'Robin', 'Klas']
};

Then you can use dot notation to acces the data:

$company.name
$company.year
#foreach($person in $company.employees)
   $person
#end

Creating Java objects in JavaScript

The "Packages" keyword and full package name must be used when creating Java objects in the JavaScript part. An example:

var now = new Packages.java.util.Date(),
    list = new Packages.java.util.ArrayList();

The variable name for the JavaScript object/value can be accessed in the Velocity part.

$now
$list