Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

SLAX Parameters Overview

Parameters may be passed to named or unnamed templates or to functions. After declaring a parameter, you can reference it by prefixing the parameter name with the dollar sign ($).

Declaring Parameters

In SLAX scripts, you declare parameters using the param statement. Optionally, you can define an initial value for each parameter in the declaration. For example:

The scope of a parameter can be local or global. Local parameters must be declared at the beginning of a block, and their scope is limited to the block in which they are declared. A parameter whose value is set by Junos OS at script initialization must be defined as a global parameter. Global parameter declarations are placed just after the style sheet declarations. A script can assign a default value to the global parameter, which is used in the event that Junos OS does not give a value to the parameter.

In a template, you declare parameters either in a parameter list or by using the param statement in the template block. Optionally, declare default values for each template parameter. If a template is invoked without the parameter, the default expression is evaluated, and the results are assigned to the parameter. If you do not define a default value in the template, the parameter defaults to an empty string.

The following named template print-host-name declares the parameter message and defines a default value:

An alternative, but equivalent, declaration is:

The template declares message and accesses its value by prefixing the parameter name with the dollar sign ($). In XSLT, the parameter name is prefixed by the dollar sign when you access it but not when you declare it.

In a function, you declare parameters either in a parameter list or by using the param statement in the function block. Optionally, you can declare default values for trailing parameters. If you invoke a function without that trailing parameter, the default expression is evaluated, and the results are assigned to the parameter. If you do not define a default value, the parameter defaults to an empty string.

The following example defines a function named size, which has three parameters: width, height, and scale. The default value for scale is 1. If the function call argument list does not include the scale argument, the calculation uses the default value of 1 for that argument. The return value for the function is the product of the width, height, and scale variables enclosed in a <size> element.

An alternative, but equivalent declaration, which uses the param statement, is:

Passing Parameters to Templates

When you invoke a template, you pass arguments into the template either in an argument list or by using the with statement. The name of the parameter supplied in the calling environment must match the name of a parameter defined in the actual template. Otherwise, the parameter is ignored. Optionally, for each parameter you pass to a template, you can define a value using an equal sign (=) and a value expression. In the following example, the two calls to the named template print-host-name are identical:

The parameter value that gets used in a template depends on how the template is called. The following three examples, which call the print-host-name template, illustrate the possible calling environments.

If you call a template but do not include a specific parameter, the default expression defined in the template is evaluated, and the results are assigned to the parameter. If there is no default value for that parameter in the template, the parameter defaults to an empty string. The following example calls the named template print-host-name but does not include any parameters in the call. In this case, the named template will use the default value for the message parameter that was defined in the print-host-name template, or an empty string if no default exists.

If you call a template and include a parameter, but do not define a value for the parameter in the calling environment, the script passes in the current value of the parameter if it was previously initialized, or it generates an error if the parameter was never declared. The following example calls the named template print-host-name and passes in the message parameter but does not include a value. If message is declared and initialized in the script, and the scope is visible to the block, the current value of message is used. If message is declared in the script but not initialized, the value of message will be an empty string. If message has not been declared, the script produces an error.

If you call a template, include the parameter, and define a value for the parameter, the template uses the provided value. The following example calls the named template print-host-name with the message parameter and a defined value, so the template uses the new value:

Example: Parameters and Match Templates

The following example matches the top level configuration hierarchy element and then instructs the Junos OS management process (mgd) to recursively apply templates to the system/host-name subtree. The parameters message and domain are used in the processing of any matching nodes.

The XSLT equivalent is:

Passing Parameters to Functions

Version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases, supports functions. Although you can use the param statement to define function parameters, you cannot use the with statement to pass parameter values into the function from the calling environment. When you call a function, you pass arguments into the function in a comma-separated list. Function arguments are passed to the function by position rather than by name as in a template.

A function declaration can define default values for trailing arguments. If there are fewer arguments in the function invocation than in the definition, the default values are used for any trailing arguments. If there are more arguments in the function invocation than in the definition, the function call generates an error.

In the following match template, the function call uses width and height data selected from each graphic/dimension element in the source XML file. The script evaluates the function, and the copy-of statement emits the return value to the result tree as the contents of the <out> element. The function call includes arguments for width and height, but not for scale. The default value of 1 is used for scale within the function block.