OpenWGA 7.0 - OpenWGA Concepts and Features

Design and development » TMLScript

Items, metadata and variables in TMLScript

Retrieving items, metadata and variables

The methods this.item() and this.meta() (or this.itemList() und this.metaList() for list items) are methods in TMLScript used to retrieve items, metadata fields and WebTML variables, just like <tml:item> and <tml:metadata> in WebTML:

this.item("body");
this.meta("title");


There are shortcut syntaxes available for retrieving item and metadata fields, which work by just retrieving the item or metadata name as a property of the TMLContext object. Spell the name in complete lowercase to retrieve an item/variable. Spell it in complete uppercase to retrieve a metadata field of that name:

this.body;
this.TITLE;


As the qualifier "this" is optional in TMLScript it actually is also ok to just use the item/var/metadata field name without it, so item and metadata fields may be retrieved like normal JavaScript variables.:

body;
TITLE;


There are a few restrictions on what items may be retrieved using the short form:

  • The item name must be suitable to be used as an object property / variable in JavaScript. For example it may not contain any characters not allowed there, like spaces.
  • The name must not be already used for a predefined method/property of TMLContext

Retrieving multivalue items and variables

There is a special behavour with retrieving items, metadatas and WebTML variables set by WebTML functionalities (like attribute var) if they contain multiple values.  In that case this.item() and this.meta() will only return the first value. This also applies to the short forms of these methods discussed before. The reason for this is that WebTML tags and items on some content platforms (Lotus Domino) always store values in the form of multivalue lists, so OpenWGA itself cannot determine if it should return a list or not.

If you need to retrieve all values of these fields you need to use this.itemList() and this.metaList() instead. These methods always return lists, even if the retrieved field has only one value (which then is the only value in the list). Even if a field is empty or not defined it will return an empty list.

This special behaviour does not apply to WebTML variables set by TMLScript. When storing a List into a WebTML variable here then it will also be retrieved as List by this.item(), so the behaviour is straight-forward here.

Setting WebTML variables

WebTML variables can also be set using TMLScript, which is - for normal variables - done via method this.setVar():

this.setVar("thevar", 1);


There is also a shortcut syntax here which sets the variable just as a property of the TMLContext object:

this.thevar = 1;


Qualifier "this" is also optional here, so setting a WebTML variable can also be done by simply setting a value to a yet undefined variable name:

thevar = 1;

Note the difference to the definition of a JavaScript variable:

var theScriptVar = 1;

This shortcut syntaxes can also be used to update existing WebTML variables of any type, like session and portlet variables.

To initially define session and portlet variables however other methods are used which to do not feature a shortcut syntax:

this.setSessionVar("theSessionVar", 1);

portlet.setVar("thePortletVar", 1);

portlet.setSessionVar("thePortletSessionVar", 1);


JavaScript variables

Of course TMLScript is also capable of creating and handling normal JavaScript variables whose declaration is introduced by the "var" statement:

var jsvar = 1;

JavaScript variables differ from WebTML variables in their scope, which is limited to the current script only. They therefor are suitable to store temporary values of calculations which should be gone once the script is finished.

Priorities of variable types

As Items, JavaScript variables and WebTML variables are sometimes retrieved using the same functionality an order of priority must exist to answer the question which type is retrieved when there are multiple equally named variables of different types.

TMLScript follows this rule of thumb: The smaller the scope, the higher the priority

Variables of lower scope are normally more specific to the current code than those with a bigger scope. So it is more likely that they are meant when their name is used.

For the known variable types this means the following priority list:

Priority Type Scope
1. JavaScript variables The current script
2. Portlet variables In the current portlet and request
3. Portlet session variables In the current portlet for the browser session
4. Normal WebTML vaiables The current request
5. WebTML session variables The browser session
6. Items- and metadata Persistent