OpenWGA 5.4 - TMLScript reference


Object:

TMLContext "this"

Description The TMLContext is the base object of the TMLScript runtime. It combines two purposes:
  • It represents the document context for which the TMLScript is called, mostly the context of the WebTML tag that executes the script. That is it points to a special content document and offers it's item and metadata fields, just like <tml:item> and <tml:meta> do in WebTML.
  • It offers the most frequently used TMLScript functionalities as methods, like for example many condition tests, context changes, and access to WGAPI objects

All methods of this object are case insensitive and can be used in any case variants, although we encourage you to use the case that is documented here.

The base TMLContext object of a script is reachable as "this" in all TMLScript code that does not belong to a TMLScript custom object (in which the "this" object is of course the TMLScript object itself).  Specifying "this" is optional. You can also just use all methods and properties of the base TMLContext without "this." as if they were global functions and variables, if their names do not collide with other variables and functions.

The TMLContext object offers a shortcut syntax for direct access to items, WebTML variables and metadata fields of the current context document.  This syntax lets you read those fields by just using their names as object properties. Specify the name in lowercase to read an item or WebTML variable, specify it in uppercase to read a metadata field. These are actually shortcuts to the methods this.item and this.meta of this object. See the examples how this is used.

You can also write to WebTML variables with this shortcut syntax when setting values to these "virtual" object properties. If there is an existing WebTML session variable of the property name it is updated. Otherwise this creates a new or updates an existing normal WebTML variable.
Retrieval As "this" object in all scripts. The "this" qualifier is optional and can be omitted, so you may also directly use TMLContext methods and properties like global functions or fields.
Allowed in script types
  • WebTML pages and normal WebTML actions
  • Master actions
  • TMLScript tasks in jobs
  • Content type events
  • Content type events
    portletevent
Properties and methods
Name Purpose
this.addWarning(message[, severe]) Adds a custom WebTML warning
tmlform.attachScaledImage(doc, scaler [, targetFileName)) Stores the image data on an ImageScaler object as attachment on a document
this.content() Providfes the WGAPI object "WGContent" which represents the current content document in context
this.contentURL([mediakey, layoutkey [, ignoreVirtualLink]]) Returns a URL to the content document in context
this.context(contextExpression [, returnContextOnError])
this.context(content)
Creates a TMLContext object for a different content document
this.cookies Represents the browser cookies of the current user
this.createEvent(name) Creates a new PortletEvent object
this.db() Returns a WGAPI database object for the database of the current context
this.document Returns the WGAPI document object in context
this.fileurl([dbkey,] containername, filename) Creates a URL pointing to a file attachment on a content document or file container
this.getSessionVar(varname) Retrieves a WebTML session variable
this.getVar(varname) Retrieves a normal WebTML variable
this.hasChildren() Returns if the current context document has child documents
this.hasLabel([container], [file], key) Tests if a label is available for the given key that can be served to the user
this.hasProfile() Returns if the user currently has a user profile
this.hdb() Returns a  WGAPI HDB object for the application in context
this.httpsession Returns the JavaEE object representing the current HTTP browser session
this.isBrowserInterface() Returns true if the WebTML page is rendered in some kind of OpenWGA authoring client like the OpenWGA Content Manager
this.isdefined(name) Tests if a name if defined as WebTML variable or item on the context document
this.isEmpty(item) Returns if the given WebTML variable or item is regarded "empty"
this.isEmptyValue(value) Returns if the given value is regarded "empty"
this.isFilled(item) Returns if the contents of an item or WebTML variable is regarded as "filled"
this.isFirstLoop([Tag-ID]) Tests if a tag <tml:foreach> is in its first iteration
this.isLastLoop([Tag-ID]) Tests if a <tml:foreach> tag is in its last iteration
this.isMainDocument() Returns if the document in context is the main document of the request
this.isNewSession() Returns if the current request is the first request in a new browser session
this.isRoot() Returns if the current context document is a root document
this.isselected() Returns if the current document is the current context document is the main document or an ancestor document of it
this.isTrue(name) Returns if the contents of an item or WebTML variable is regarded "true"
this.isWebEnvironment() Returns if the current TMLScript code is executed in the context of a WebTML request
this.item([type, ] itemname) Returns the value of an item or a WebTML variable
this.itemList([type, ] itemname) Returns the value of an item or WebTML variable as list
this.label([[container, ] file, ] key [, params])
this.label(database, container, file, key , params [, returnPlaceholder])
Returns a WebTML label in a language that the user should understand
this.lastError Returns the error message of the last failed context expression that was done from this context object
this.layoutURL(medium, layout) Generates a URL that calls a WebTML module without document context
this.log Provides an object for logging output to the application log
this.maincontext Returns the main context of the request
this.meta([type,] name) Returns the value of a metadata field
this.metaList([type,] metaname) Returns the value of a metadata field as list
this.option(name [, defaultValue]) Retrieves the value of a WebTML option
this.path Returns an absolute context path to the current context document
this.portlet Returns a portlet object for the current WebTML portlet
this.profile Returns the users personalisation profile as profile object
this.removeSessionVar(varname) Removes a WebTML session variable
this.removeTMLForm(formid) Removes the form of the given ID from form registry
this.removeVar(varname) Removes a (normal) WebTML variable
this.request Returns the JavaEE object representing the current HTTP request
this.response Returns the JavaEE object representing the current HTTP response
this.saveProfileOnEnd() Delays saving of the personalisation profile until the end of the request
this.setOption(name, value [, scope]) Sets a WebTML option
this.setSessionVar(varname, value [, allowSerialisation]) Sets or updates a WebTML session variable
this.setVar(varname, wert) Creates or updates a (normal) WebTML variable
this.tagInfo(tagid, infoname) Retrieves a WebTML tag information
this.tmlform Returns the TMLForm object representing the current WebTML form
this.tmlformbyid(id) Returns a TMLForm object representing the WebTML form of the given ID
Examples Reading an item "body" in three different ways. First in full syntax using the method "item". Then in shortcut syntax using the item name as object property in lowercase. Then even shorter, leaving out the optional "this" reference:
return this.item("Body");
return this.body;
return body;

Using the full method "meta" you can also retrieve non-content metadata, like the database key in this example:
return this.meta("db", "dbkey");

Retrieving a different context. This is again a full TMLContext object which points to a different document, in that case the root document of the base TMLContext's document:
var rootContext = this.context("root");

Using the shortcut syntax to set WebTML variables: When you omit the "var" operator when setting a variable you effectively create or update a WebTML variable, which then can also be retrieved via <tml:item/>
<tml:script>
  aList = createList();
  aList.add("a");
  aList.add("b");
</tml:script>

<tml:item name="avar"/>

If there is an existing WebTML session variable of the used name then this syntax updates it. Therefor the line "sessionvar++" effectively modifies the session variable set before:
  setSessionVar("sessionvar", 1);
  sessionvar++;