BLOG CATEGORIES



Javascript functions to create complex script expressions in RPE

Using conditions and filters in Rational Publishing Engine is quite necessary. Knowing how to create a condition or a filter in RPE it’s a must if you want to have a decent template. The big problem for the standard RPE user in creating such a filter it’s the Java Script language.

A filter/condition is a script expression, and there is a specialized editor in RPE – Script Expression Editor that allow the user to create such expression. The problem is that the “condition list” from that editor is limited, and in order to create complex conditions and filters, a RPE user need to have a basic knowledge of Java Script.

“I am a newbie to this application. It’s a little annoying that
when you want to use “contains” in a filter, it isn’t in
the condition list.”
RPE Developer Works Forum

In this post I’ve decided to present some top-used Javascipt function for strings.

Why for strings?

Because in RPE all the data attributes and variables from conditions and filters (RPE filter) are treated as string objects.

At the end of the presentation I’ll show you how to use Javascript functions to create script expressions in Rational Publishing Engine.

Javascript Functions:

indexOf(searchstring,start)

searchstring The string to search for Required
start The start position in the string to start the search. If omitted, the search starts from position 0 Optional

One of the most widely used string methods is indexOf. This method lets you check for a character or even a small string within the current string, and returns the position at which your character or string begins. The position starts counting at 0, so if indexOf returns zero, it means your character or string begins at the 1st character.
Also, indexOf has a useful return value of -1 if the character or string you searched for is not contained within the string. And finally, if the character or string you search for occurs more than once, indexOf returns only the first appearance of your character or string.

lastIndexOf(searchstring,start)

searchstring The string to search for Required
start The start position in the string to start the search. If omitted, the search starts from position 0 Optional

There’s also lastIndexOf which gives the last occurrence of the character or combination. It otherwise works exactly as indexOf.

charAt(index)

index An integer between 0 and string.length-1 Required

Using the charAt method, you can find out what character is filling a designated position within a string. Basically, it allows you to find out what character is first, second, third, and so on. The important thing to remember is that when dealing with the position of a character, the count starts at zero, not one. So the first character is actually at position zero.

length

This method returns the length of a string.

split(separator,limit)

separator Specifies the character to use for splitting the string. If omitted, the entire string will be returned Optional
limit An integer that specifies the number of splits Optional

It allows you to split a string at the places of a certain character. You must put the result in an array, not in a simple variable.

substring (from_index,to_index)

from_index The index where to start the extraction. First character is at index 0 Required
to_index The index where to stop the extraction. If omitted, it extracts the rest of the string Optional

The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.
This method extracts the characters in a string between “from” and “to”, not including “to” itself.

toLowerCase & toUpperCase

The toLowerCase() method converts a string to lowercase letters.
The toUpperCase() method converts a string to uppercase letters.

How to use javascript functions to create complex conditions in RPE

Let’s think at this scenario:

We want to display the first three characters of a string variable. If the string has less than 3 characters, then the string will remain as it is.
To construct such a script we need to functions: substring() and length(). Considering that our internal RPE variable is _documentTitle the script expression will be:

Script Expression Editor

Figure 1: Script Expression Editor

Leave a Reply

(required)

(required)