This page covers a few other features of WebStringTemplates. These
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
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.
Some example sub-templates are catalogRowRed.st, catalogRowGreen.st and catalogRowBlue.st. Replacing the catalogTable.st with
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.
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.
In catalogRowColored.st the color is accessed via the name used in the argument list. E.g.
<!-- process row of the catalog -->
(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
$[url]$ and you can access
the string "Add to Cart" using
$[item]$ will return the
entire processed output of the sub-template
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.
Often you want to vary the display depending on what data is available.
You use the
$[if( )]$ ...
$[endif]$ statements to
The text inside the
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.
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 -->
<!-- on-sale -->
<!-- else not on-sale -->
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.
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
)]$which will show you exactly what the $[if(test)]$ statement
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.
<!-- catalog book table -->
<!- open table and add header -->
<th><font size="+1" color="red">On
<!-- process row of the catalog -->
<!- close table -->
<!-- end of catalog book table -->
Then in bookcatalogTable.st.html, the statement
is changed to
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
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
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("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.
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.
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.