IBM Books

Net.Data Programming Guide


Generating HTML in a Macro

Net.Data lets you easily present standard HTML on the application user's browser. The following blocks show you how to format HTML in Net.Data macros.

HTML Blocks

The HTML block and the functions which get invoked from an HTML block are the blocks of the Net.Data macro which generate HTML output to a browser. Whenever Net.Data is invoked, an HTML block must be specified. What is contained in this block controls the rest of the Net.Data invocation.

Any valid HTML may appear in an HTML block. In addition, INCLUDE statements, function calls, and variable references can be in an HTML block. A common use of HTML blocks in a Net.Data macro is shown by this sample Net.Data macro:

%DEFINE DATABASE="MNS96"
 
%HTML(INPUT){
<H1>Hardware Query Form</H1>
<HR>
<FORM METHOD="POST" ACTION="/cgi-bin/db2www/equiplst.mac/report">
<dl>
<dt>What hardware do you want to list?
<dd><input type="radio" name="hdware" value="MON" checked>Monitors
<dd><input type="radio" name="hdware" value="PNT">Pointing devices
<dd><input type="radio" name="hdware" value="PRT">Printers
<dd><input type="radio" name="hdware" value="SCN">Scanners
</dl>
<HR>
<input type="submit" value="Submit">
</FORM>
%}
 
%FUNCTION(DTW_SQL) myQuery() {
SELECT MODNO, COST, DESCRIP FROM EQPTABLE WHERE TYPE=$(hdware)
%REPORT{
<B>Here is the list you requested:</B><BR>
%ROW{
<HR>
$(N1): $(V1)    $(N2): $(V2)
<P>
$(V3)
%}
%}
%}
 
%HTML(REPORT){
@myQuery()
%}

You can invoke the Net.Data macro from an anchor reference like this:

<a href="http://www.ibm.com/cgi-bin/db2www/equiplst.mac/input">List of hardware</a>

When the application user clicks on this reference, Net.Data is invoked, and Net.Data parses the Net.Data macro file. When it gets to the HTML block specified on the invocation, in this case the HTML(INPUT) block, it begins to process the text inside the block. Anything that is not recognized by Net.Data as a Net.Data macro language construct is assumed to be HTML, and is sent to the browser to be displayed.

After a selection is made and the Submit button pressed, the ACTION part of the HTML FORM element is executed, which specifies a call to the Net.Data macro's HTML(REPORT) block. The HTML(REPORT) block is then processed just as the HTML(INPUT) block was. All data up to the queryHardware() function call is output to the browser as HTML.

The queryHardware() function call is then processed, which in turn invokes the SQL FUNCTION block. After the $(hdware) variable reference is replaced in the SQL statement with the value returned in the input form, the query is executed. At this point, Net.Data again starts sending HTML to the browser, displaying the results of the query according to the HTML specified in the REPORT block.

After the REPORT block processing is done, we return again to the HTML(REPORT) block, and finish processing by sending out the remaining HTML specified after the queryHardware() function call.

Only one HTML block is processed for each Net.Data invocation. However, by using HTML anchor references and forms, it becomes very easy to let the person using an application start another invocation of Net.Data on another HTML block, all controlled by you.

Report Blocks

The REPORT block is used to format and display data output from a FUNCTION block. This output is typically table data, although any valid combination of HTML tags, macro variable references, and function calls may be specified. A table name may be specified on the REPORT block, but is not required. If a table name is not specified, the table data used is that of the first output table in the parameter list of this FUNCTION block. If no table was specified on the FUNCTION block, the default table data is used.

The REPORT block is composed of three parts, each of which is optional:

If you do not want to display any table output from the ROW block, just leave it empty.

When Net.Data processes a FUNCTION block, a call is made to a language environment and data is returned. Net.Data then processes the REPORT block.

Inside the REPORT block, Net.Data provides several implicitly-defined variables to let you access the data in the Net.Data macro results table. These variables are described in Table 1. For additional detail, see the Report Variables section in the Reference Guide.

Header and footer information is not explicitly specified as such in a REPORT block. Net.Data simply processes everything it finds before a ROW block as header information, and everything it finds after the ROW block as footer information. As with the HTML block, the Net.Data macro processor treats everything in the header, ROW, and footer blocks as HTML and sends that data to the browser. You can also include functions and variables.

To avoid the report, omit the REPORT block and set DTW_DEFAULT_REPORT to NO. If you set it to YES, a report is shown with the default format, for example:

 SHIPDATE   | RECDATE    | SHIPNO   |
-------------------------------------
 25/05/1997 | 30/05/1997 | 1495194B | 
-------------------------------------
 25/05/1997 | 28/05/1997 | 2942821G |
-------------------------------------           

Set DTW_HTML_TABLE to YES to use HTML table tags instead of preformatted text for the default report.

This example shows how you can customize report formats using special variables and HTML tags. It displays the names, phone numbers, and fax numbers from the table CustomerTbl:

%FUNCTION(DTW_SQL) custlist() {
     SELECT Name, Phone, Fax FROM CustomerTbl
   %REPORT{
<I>Phone Lookup Results:</I>
<BR>
=====================
   %ROW{
   Name: <B>$(V1)</B>
Phone: $(V2)
Fax: $(V3)
------------------------------
   %}
   Total records retrieved: $(ROW_NUM)
   %}
   %}

The resulting report looks like this in the Web browser:

Phone Query Results:
====================
Name: Doen, David
Phone: 422-245-1293
Fax: 422-245-7383
------------------------------
Name: Ramirez, Paolo
Phone: 955-768-3489
Fax: 955-768-3974
------------------------------
Name: Wu, Jianli
Phone: 525-472-1234
Fax: 525-472-1234
------------------------------
Total records retrieved: 3

Net.Data generated the report by:

  1. Printing Phone Query Results: once at the beginning of the report.

  2. Giving the variables V1, V2, and V3 the values for Name, Phone, and Fax respectively for each row as it is retrieved.

  3. Drawing a line after each row retrieved to help readability.

  4. Printing the string Total records retrieved: and the value for ROW_NUM once at the end of the report.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]