In this article we will look into how to use the variables in RTF template.
When we work on RTF templates to create letters/payslip modifications we will have to use some variables to store temporary values and then use them later on in the template.
We use the functions set_variable and get_variable to set and get variable values.
Table of Contents
set_variable function:
This function is used to declare a variable and assign value to it based upon some other field or a default value based on the requirement.
Syntax of set_variable:
<?xdoxslt:set_variable($_XDOCTX, 'variable name', value)?>
The $_XDOCTX specifies the global document context for the variables.
variable name – This is the variable name
value – This is the value that will be assigned to the variable. It can either be a constant value or any XPATH command.
Sample Set_variable commands:
We need to create a form field and add the below code to the form field.
<?xdoxslt:set_variable($_XDOCTX, 'max_hire','1901-01-01')?>
<?xdoxslt:set_variable($_XDOCTX, 'x', xdoxslt:get_variable($_XDOCTX, 'x' + 1)?>
<?xdoxslt:set_variable($_XDOCTX, ‘LC’, xdoxslt:get_variable($_XDOCTX, ‘LC’) + 1)?>
<?xdoxslt:set_variable($_XDOCTX, ‘tot_lines’, count(.//SNO))?>
<?xdoxslt:set_variable($_XDOCTX,’counter’,0)?>
get_variable function:
This function is used to get the value of a variable that has already been defined in the template.
Syntax of get_variable:
<?xdoxslt:get_variable($_XDOCTX, 'variable name')?>
Sample get_variable commands:
<?xdoxslt:get_variable($_XDOCTX, 'V_SNO')?>
<?format-date:xdoxslt:get_variable($_XDOCTX, 'max_hire'); 'DD-MMM-YYYY'?>
<?xdoxslt:get_variable($_XDOCTX, 'logical_date')?>
Sample usecase for using variable to get the running totals:
/*initialize a variables*/
<?xdoxslt:set_variable($_XDOCTX, 'counter', 0)?>
/*update the variable's value by adding the current value to SEQ, which is XML element */
<?xdoxslt:set_variable($_XDOCTX, 'counter', xdoxslt:get_variable($_XDOCTX, 'counter') + SEQ)?>
/* accessing the variables */
<?xdoxslt:get_variable($_XDOCTX, 'counter')?>
/*Working in a loop*/
<?xdoxslt:set_variable($_XDOCTX, 'counter', 0)?>
<?for-each:G2?>
/*increment the counter*/
<?xdoxslt:set_variable($_XDOCTX, 'counter', xdoxslt:get_variable($_XDOCTX, 'counter') + 1)?>
<?end for-each?>
<?xdoxslt:get_variable($_XDOCTX, 'counter')?>