Forward Logo (image)       WebStringTemplates
      Previous Back Back Back Next

 
WebStringTemplates for the Web Page Designer

A Few Other Features

This page covers a few other features of WebStringTemplates. These are:-
Alternating Templates
Template Arguments
If Statement and the Not Operator
Using list_first and list_last
The Only Other Operator - String Concatenation
Escape Sequences and Non-English Web Pages
A Few Other Features

Alternating Templates

When displaying lists of things, you may want to change the color or other formatting instructions depending on the list position. For example, you might want to alternate the color of the background for the elements of a list. You can do this with WebStringTemplates by supplying more then one sub-template (separated by commas) after the colon. I.e.

$[booksWithFmtPrices:catalogRowRed(),catalogRowGreen(),catalogRowBlue()]$

Some example sub-templates are catalogRowRed.st, catalogRowGreen.st and catalogRowBlue.st. Replacing the catalogTable.st with

<table>
$[booksWithFmtPrices:catalogRowRed(),catalogRowGreen(),catalogRowBlue()]$
</table>

Gives the this output. (In manual/completedExamples, this example is bookcatalogRGB.st.html.) Clearly you need to change the color of the links if you are to use these row colors.

Template Arguments

These three row templates only differ in their background color, so you could use just one template and pass the background color as a parameter. As shown in bookcatalogColored.st.html.txt.

$[booksWithFmtPrices:catalogRowColored(color="#ff0000"), catalogRowColored(color="#00ff00"), catalogRowColored(color="#0000ff")]$

In catalogRowColored.st the color is accessed via the name used in the argument list. E.g.

<!-- process row of the catalog -->
<tr bgcolor=$[color]$>

(In the manual/completedExamples this example is bookcatalogColored.st.html.)

You can have more than one argument, just separate them with commas (,). Argument values can be either strings ".." or attribute paths (which return a string) or even another template (which also returns a string). E.g.

$[insertUrl( url=cart.addUrl, text="Add to Cart" )]$

$[boldMe( item=copyrightNotice()) ]$

In insertUrl.st you can access the string value of cart.addUrl using $[url]$ and you can access the string "Add to Cart" using $[text]$ i.e.

<a href="$[url]$">$[text]$</a>

In boldMe.st, $[item]$ will return the entire processed output of the sub-template copyrightNotice.st

Notice that you don't need to put $[ ]$ around cart.addUrl in the first example. Once you are inside $[ ]$, anything that is not a string( " " ), sub-template( () ) or argument name( name= ) is assumed to be a data path.

If Statement and the Not Operator

Often you want to vary the display depending on what data is available. You use the $[if( )]$ ... $[endif]$ statements to do this.

The text inside the

$[if(test)]$
..
$[endif]$

is included in the output if the value of test is not one of the strings:- ""(empty string), "false", "0" or missing (ie no data). These values are false and every thing else is true.

At present there is no ELSE statement. To get the else part you need to use the not operator ! with another $[if( )]$ statement.

$[if(!test)]$
..
$[endif]$

In the catalog.data.html file some books were on sale, as indicated by bookRow.onSale being non-zero. In these cases we want to show the sale price in the catalog page. Going back to the plain, uncolored, catalog page, you can change the catalogRow.st template to

<!-- process row of the catalog -->
<tr><td>
<a href="$[attr.detailsUrl]$">$[attr.bookRow.title]$</a> $[By]$ $[attr.bookRow.surname]$</td>
<!-- on-sale -->
$[if(attr.bookRow.onSale)]$
<td><strike>$[attr.fmtPrice]$</strike></td>
<td><font color="red">$[attr.fmtSalePrice]$</font></td>
$[endif]$
<!-- else not on-sale -->
$[if(!attr.bookRow.onSale)]$
<td>$[attr.fmtPrice]$</td>
<td>&nbsp;$</td>
$[endif]$
<td>&nbsp; &nbsp; <a href="$[addUrl]$">$[CartAdd]$</a>
</td></tr>

and add an extra column to the table headings in catalogTable.st. (In manual/completedExamples, this template is called bookcatalogIf.st.html.) The output page now looks like this.

Catalog Page

Remember you can do all these changes interactively while RunWST is running and just refresh your browser to see the updated output.

If your $[if(test)]$ does not seem to be working, it may be that your data path for the test is incorrect. If the data cannot be found, the $[if(test)]$ evaluates to false. If in doubt you can check the value of the test using
$[(test)]$
which will show you exactly what the $[if(test)]$ statement is testing.

Using list_first and list_last

As mentioned in Processing a List, when a list is processed using the : syntax, there are three additional attributes defined in the sub-template, list_i, list_first and list_last. These are in addition to the attr attribute which holds the list element. The files bookcatalogTable.st.html and catalogFullTable.st are provided in the completedExamples sub-directory as an example of using these attributes to add a table header and table end to a list of elements.

In catalogFullTable.st, the code from catalogRow.st is used to display the rows of the table, but the following $[if( )]$ statements are added above and below the code to open the table before processing the first row and close the table after processing the last row.

$[if(list_first)]$
<!-- catalog book table -->
<!- open table and add header -->
<table>
<tr>
<th><font size="+1">Books</font></th>
<th><font size="+1">Price</font></th>
<th><font size="+1" color="red">On Sale</font></th>
<th></th>
</tr>
$[endif]$

<!-- process row of the catalog -->
.....
</td></tr>

$[if(list_last)]$
<!- close table -->
</table>
<!-- end of catalog book table -->
$[endif]$

Then in bookcatalogTable.st.html, the statement

$[catalogTable()]$
is changed to
$[booksWithFmtPrices:catalogFullTable()]$

to pass the list directly to catalogFullTable.st for processing. The output page is the same as shown above.

If you pass an argument to a sub-template, using a statement like

$[bookRows(booksWithFmtPrices)]$

then even though booksWithFmtPrices is a list, none of the list attributes (list_i, list_first or list_last) are defined because you are not processing the list element by element using the : syntax, but rather just passing the whole list to the attr attribute in the bookRows.st sub-template. Of course since bookRows.st now has the whole list in attr, it can process it element by element using a statement like

$[attr:row()]$

The Only Other Operator - String Concatenation

Apart from the not (!) operator, there is only one other operator in WebStringTemplate, the "string concatenation" operator, +. You can use this operator inside template statements to add strings together to form arguments or to form a test for $[if(test)]$. E.g.

$[insertUrl(url="/faq/view?ID="+faqid, title=faqtitle)]$

$[if("fa" + "lse")]$

The second example evaluates to $[if("false")]$ and being false, the text in the if block is not included in the template output.

Escape Sequences and Non-English Web Pages

The template is read by RunWST using the ISO-8859-1 character encoding (Latin-1). If you want to include a character that is not in this character set then you need to use a Unicode escape sequence, \udddd. If you want include a \ in your text you need to use \\ (but see below for special escape sequences for use inside strings inside template statements).

If you are desiging a web page in a non-english language, you will need to add another step to the process in order to encode the non-ascii characters to Unicode escape sequences. Sun provides a utilitiy, native2ascii, with the Java Software Development Kit (SDK), which will translate non-ascii characters to Unicode escape sequences (\udddd). So you would design your web page template using your computer's usual non-ascii character set and then save it as, say, bookstore.fr.html and then run native2ascii to produce bookstore.st.html which RunWST will process to produce the output web page. If you need, native2ascii, ask the server programmer or system architect of a copy of it.

If you ever need to output $[ or ]$ in a page, then you can use the template statements

$[ "$[" ]$ and $[ "]$" ]$

When using strings ("..") inside a template statement (that is inside $[ ]$), you can use the following escape sequences:-

\\" for a "
\\\\ for a \ (yes four (4) \)
\\n for a new line
\\r for a carriage return
\\t for a tab
\\b for a backspace
\\f for a formfeed

See Data Interface Definition for details on escape sequences used in the Data Interface Definition file.

A Few Other Features

There are a few other statement variations that WebStringTemplates inherits from Terence Parr's StingTemplate, which are not described here. The ones described above should be sufficient for your needs, but if you are interested refer to Terence's documentation.


Forward Logo (image)       WebStringTemplates
      Previous Back Back Back Next

Contact Forward Computing and Control by
ęCopyright 2004 Forward Computing and Control Pty. Ltd. ACN 003 669 994