Flow Constructs
Rendering information will be relaying on numerous conditions like security, hierarchy, date and time and other. Every macro implements the shared macro parameter Condition to conditionally execute the macro.
Smartsite SXML | Copy Code |
---|---|
<se:text condition="datetime.getdayofweek(datetime.now())==1">Today is Monday</se:text> |
The Condition parameter is an expression which will be evaluated. The input for the expression depends often on the result of other macros. Because of the Conditon parameter must be an attribute, it is hard to add a macro to the value of the Condition parameter. However, by saving a macros result in a buffer and reading this buffer using vipers, passing data between macros is easy:
Smartsite SXML | Copy Code |
---|---|
<se:text save="resultoffirstmacro">1</se:text> <se:text condition="buffer.get(resultoffirstmacro)==1">The value of the first macro is one</se:text> <se:text condition="buffer.get(resultoffirstmacro)==2">The value of the first macro is two</se:text> |
When two or more cases are dependent on the saved value, it starts to be rather ineffective if page-logic is handled this way. In the example above, the second macro reading the buffer should be parsed and evaluated on the Condition parameter. To avoid this overhead, branching macros can be used.
Branching Macros
Branching macros like the If and Switch macro give a result based on the expression passed. This expression has to evaluated once the result dictates which branch is token to be evaluated further.
If
Smartsite SXML | Copy Code |
---|---|
<se:if expression="some expression"> <se:then> Data to show/execute when expression evaluates to true </se:then> <se:else> Data to show/execute when expression evaluates to false </se:else> </se:if> |
Switch
Smartsite SXML | Copy Code |
---|---|
<se:switch expression="some expression"> <se:case value="some value"> Data to show/execute when expression evaluates to the given value </se:case> <se:case value="some other value"> Data to show/execute when expression evaluates to the given value </se:case> <se:case> Data to show/execute when no match is made </se:case> </se:switch> |
The switch statement also allows for expressions in each case, so multiple expression evaluations can be done using one switch macro:
Smartsite SXML | Copy Code |
---|---|
<se:switch expression="some expression"> <se:case expression="some other expression"> Data to show/execute when this case's expression evaluates to true </se:case> <se:case value="some other value"> Data to show/execute when switch expression evaluates to this case's value </se:case> <se:case> Data to show/execute when no match is made </se:case> </se:switch> |
Normally, when a case value or expression matches, the remaining cases are dropped and execution of the switch macro is terminated. The optional break attribute however can be used to force continuation of the remaining cases:
Smartsite SXML | Copy Code |
---|---|
<se:switch expression="some expression"> <se:case expression="some other expression" break="false"> Data to show/execute when this case's expression evaluates to true </se:case> <se:case value="some other value"> Data to show/execute when switch expression evaluates to this case's value </se:case> <se:case> Data to show/execute when no match is made </se:case> </se:switch> |
Flow Macros
Flow macros are used for executing code as long a certain condition is matched.
For
The For macro executes some code a number of times
Smartsite SXML | Copy Code |
---|---|
<se:for from="97" to="122"> Ascii {this.index()}: {char.fromint(this.index())} <br /> </se:for> |
DoWhile
The DoWhile macro executes code and continues executing while some condition is true. In other words, the expression is evaluated after the execution of the inner-Xml
Smartsite SXML | Copy Code |
---|---|
<se:dowhile expression="buffer.get(a) LTE 10" timeout="0.1"> {buffer.set(a, convert.toint(buffer.get(a, default=0))+1)} {buffer.get(a)} </se:dowhile> |
It is very sensible to add a timeout parameter the DoWhile macro and precisely implement the expression. If no timeout parameter is given and the macro fails to bail out, the macro stops executing once the default timeout of 5 seconds has elapsed.
While
The While macro executes code when condition is true continues executing as long the condition is true.
Smartsite SXML | Copy Code |
---|---|
<se:while expression="buffer.get(a, default=0) LTE 10" timeout="1"> {buffer.set(a, convert.toint(buffer.get(a, default=0))+1)} {buffer.get(a)} </se:while> |
The difference between the While and DoWhile macro can be easily showed using next example:
Smartsite SXML | Copy Code |
---|---|
<se:dowhile expression="1==2"> Do this! </se:dowhile> <hr /> <se:while expression="1==2"> Do this! </se:while> |
In the example above, only the first macro gives a result because the expression is tested afterwards.