Skip to main content

 
IBM Power Systems software  >  IBM i  > Software  > 

Net.Data for i5/OS

  
Overview News Library Education Support
Net.Data Macro Language Enhancements: Nested Ifs, While Loops, and Much More!

The Net.Data macro language has been enhanced to include the ability to nest if-statements, perform loops, and call a user-defined function that is not tied to a language environment. These enhancements are currently only available for the AS/400 version of Net.Data. The details of the enhancements are documented below.

Integer Comparisons
This enhancement will allow terms in an IF or WHILE condition to be treated as numeric if they are both integers. An integer comparison will be done if the following conditions are true:

  • the condition is a binary operation (<, >, <=, >=, !=, ==).
  • both terms in the condition represent integers. This means the terms are strings of digits, optionally preceded by a '+' or '-' character. The string cannot contain any non-digit characters other than the '+' or '-'. Some valid strings are:
    +1234567890
    -47
    000812
    92000
    

    The following strings are not valid:

    -  20     (contains blank characters)
    234,000   (contains a comma) 
    57.987    (contains a decimal point)
    

If either of the terms in a condition does not represent an integer, then a normal string comparison will be done. For example:

      %IF (blockSize < "1024")
         @getFreeBlock(blockSize)
      %ELIF (blockSize < "4096")
         @allocateNewBlock(blockSize)
      %ENDIF

%MACRO_FUNCTION
This construct allows a user to define a function which will execute a block of macro language statements. The MACRO_FUNCTION is invoked with a function call, exactly the same as a normal FUNCTION block.

Syntax

>>-%macro_function--
                          function_name-->
>--| parameter definition |-->
>--{--| function body |--%}--><
 
parameter definition
 
|--(--+----------------------+--)-------------|
      |  +-,--------------+  |
      |  V                |  |
      +---+-------+-
                          name--+--+
          +-IN----+
          +-OUT---+
          +-INOUT-+
 
function body

  +-----------------------+ 
  V                       |
|---+---------------------+-+--------------+--|
    +-
                          variable reference-+  +-
                          report block-+
    +-
                          HTML if block------+
    +-
                          function call------+
    +-
                          HTML statement-----+
    +-
                          include statement--+
    +-
                          WHILE block--------+
 
                    

Context
The MACRO_FUNCTION block can be found in these contexts:

  • Macro IF block
  • Outside of any block in the declaration part of the Net.Data macro.

Restrictions
The MACRO_FUNCTION block can contain these elements:

  • HTML IF block
  • INCLUDE statement
  • INCLUDE_URL statement
  • REPORT block
  • WHILE block
  • Variable references
  • Function calls
  • HTML source

Examples

Example 1:

%MACRO_FUNCTION setMessage(IN rc, OUT message) {
%IF (rc == "0")
  @dtw_assign(message, 
              "Function call was successful.")
%ELIF (rc == "-1")
  @dtw_assign(message, 
              "Function failed, out of memory.")
%ELIF (rc == "-2")
  @dtw_assign(message, 
              "Function failed, invalid parameter.")
%ENDIF
%}

Example 2:

%MACRO_FUNCTION setup(IN browserType) {
%{ 
call this function at the top of each HTML block 
in the macro 
%}
%INCLUDE "header_info.html"

@dtw_rdate()
%IF (browserType = "IBM")
  @setupIBM()
%ELIF (browserType = "MS")
  @setupMS()
%ELIF (browserType = "NS")
  @setupNS()
%ELSE
  @setupDefault()
%ENDIF
%}

Example 3:

%MACRO_FUNCTION printReport(INOUT table) {
%REPORT {
%ROW { %}
$(N1) $(N2) $(N3)
$(V1) $(V2) $(V3)
%}
%}

Notes

  • A language environment type is not specified on a MACRO_FUNCTION block. The MACRO_FUNCTION does not go through the language environment interface - it executes directly in the frontend.
  • The RETURNS clause is not allowed in a MACRO_FUNCTION block.
  • The REPORT block is optional, just like in a FUNCTION block. If a REPORT block is specified, and an OUT or INOUT table parameter is not passed to the MACRO_FUNCTION, the REPORT block will not be executed. If more than one OUT or INOUT table parameter is passed to the function, the first one specified in the parameter list will be used for the REPORT block processing.
  • The MESSAGE block is not allowed in a MACRO_FUNCTION block.
  • The implicit RETURN_CODE variable is not set by a MACRO_FUNCTION block.
  • Unlike a FUNCTION block, where variable references and function calls are processed before the rest of the executable statements, the executable statements section of a MACRO_FUNCTION block is processed all at once, as if it were in-line in the macro where the function call was made.

Nested %IF Statements
%IF statements are now allowed inside other %IF statements. For example:

   @calculateSize(blockSize)
   %IF (blockSize != "0")
      %IF (blockSize < "1024")
         @getFreeBlock(blockSize)
      %ELIF (blockSize < "4096")
         @allocateNewBlock(blockSize)
      %ELSE
         %IF (hardError = "YES")
            @printError()
         %ELSE
            @allocateNewBlock("4096")
         %ENDIF
      %ENDIF
   %ELSE
      @printError()
   %ENDIF

%While Statements
The WHILE block provides a looping construct based on conditional string processing. You can use the WHILE block in the HTML block, the REPORT block, the ROW block, the HTML IF block, the MACRO_FUNCTION block, and the WHILE block.

Syntax

>>-%while--| condition list |--{-->

  +-------------------------+
  V                         |
>----+--------------------+-+--%}---><
     +-
                          variable reference-+
     +-
                          function call------+
     +-
                          string-------------+
     +-
                          HTML statement-----+
     +-
                          HTML if block------+
     +-
                          WHILE block--------+
 
 
condition list
 
|--(--+-(--
                          condition list--)----------------+-)--|
      +-
                          condition list--&&--
                          condition list--+
      +-
                          condition list--||--
                          condition list--+
      +-!--
                          condition list-------------------+
      +-| condition |-----------------------+
      +-| term |----------------------------+
 
condition
 
|--
                          term--+-<--+-
                          term----------------------|
         +->--+
         +-<=-+
         +->=-+
         +-!=-+
         +-==-+
 
term
 
|--+-
                          variable reference-+--------------------------|
   +-"--
                          string--"-------+
   +-
                          variable name------+
   +-
                          function call------+
 
                    

Context
The WHILE block can be found in these contexts:

  • HTML block
  • REPORT block
  • ROW block
  • MACRO_FUNCTION block
  • HTML if block
  • WHILE block

Restrictions
The WHILE block can contain these elements:

  • HTML if block
  • WHILE block
  • strings
  • HTML statements
  • Function calls
  • Variable references

Examples

Example 1:

%DEFINE loopCounter = "1"

%HTML(build_table) {
%WHILE (loopCounter <= "100") {
  %{ generate table tag and column headings %}
  %IF (loopCounter == "1")
 
%ENDIF %{ generate individual rows %} %{ generate end table tag %} %IF (loopCounter == "100")
Item # Description
$(loopCounter) @getDescription(loopCounter)
  %ENDIF

  %{ increment loop counter %}
  @dtw_add(loopCounter, "1", loopCounter)
%}
%}

Notes

  • The condition list of a WHILE block has exactly the same syntax as the condition list for an IF block. The condition list will be evaluated each time through the loop, and the executable statements inside the WHILE block will be processed until the condition becomes false.
  • WHILE blocks and IF blocks may be nested within each other.

Comments Anywhere
Comments are now allowed anywhere between parsable tokens in the macro language.

Issuing Function Calls
Function calls are now allowed in IF and WHILE conditions and in function call argument lists. Examples:

%IF (@dtw_radd(counter, "1") == "10")
   ...
%ENDIF

%HTML {
@myFunc(@myOtherFunc(), @yetAnotherFunc())
 ...
%}

NOTES:

  1. Function calls evaluate to a string, so in the case where a function call is specified in a function call argument list, the argument the function call is being specified for must be an INPUT only parameter.
  2. A function call which does have a RETURNS clause or is not a dtw_r type of built-in will evaluate to an empty string ("").

Variable References
Variable references are now allowed in function call argument lists. Example:

%HTML {
@myFunc($(var1), $(var2))
 ...
%}
As with function calls, variable references evaluate to a string, so they can only be specified for arguments which are INPUT-only.

#INCLUDE in Message Blocks
The INCLUDE statement is now allowed anywhere inside a MESSAGE block. This will allow the user to easily include sets of common messages or to include large amounts of HTML in message blocks. Examples:

%MESSAGE {
  %INCLUDE "commonMsgs.inc"
%}

%MESSAGE {
  100: %INCLUDE "repost.htm": continue
%}

Default Message Variable
You can now access the new Net.Data variable DTW_DEFAULT_MESSAGE after a call to a function that is tied to a language environment when an error occurs to access the error message, if any, returned by a language environment. Example:

%MESSAGE {
  100: "Message is $(DTW_DEFAULT_MESSAGE)": continue
%}