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.
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
|
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!
Global variables declared in the JavaScript part are accessible in the Velocity part. An example that uses the Sitevision Public API is shown below:
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
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: 2020,
employees: ['Magnus', 'Robin', 'Klas']
};
Then you can use dot notation to access the data:
$company.name
$company.year
#foreach($person in $company.employees)
$person
#end
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
Important Note! Only explicitly whitelisted Java classes can be created! See the Java Whitelist page for all available Java classes