3.2.6. parametrize()

array parametrize ( string $instruction, array $args, array &$config [, int $style = OPT_STYLE_BOTH] )

This method is used for the instruction parameter parsing. The first parameter defines the instruction name (used only, if an error occured and the compiler needs to tell, where it is). $args is an argument array got from the template tree. $config is a reference to the array that defined the number and type of parameters we need. After the method execution, the parsed values overwrite the configuration in this array. The format of this array is:

$params = array(
	'param_1' => array(OPT_PARAM_REQUIRED, OPT_PARAM_ID),
	// ...
	'param_n' => array(OPT_PARAM_OPTIONAL, OPT_PARAM_EXPRESSION, NULL),
	// ...
);

The index defines the parameter name and the value is the parameter configuration. Their elements mean:

  1. Whether the parameter is required (OPT_PARAM_REQUIRED) or not (OPT_PARAM_OPTIONAL). Optional parameters must be provided after the required ones.
  2. Parameter type:
    1. OPT_PARAM_ID - any valid identifier, for example "foo", "bar32".
    2. OPT_PARAM_EXPRESSION - the value is an OPT expression, for example "$a + $b"
    3. OPT_PARAM_ASSIGN_EXPR - like above, but also assignment allowed.
    4. OPT_PARAM_STRING - the value is treated as a string
    5. OPT_PARAM_NUMBER - the value is treated as a number
    6. OPT_PARAM_VARIABLE - the value must begin with "@" and it is treated as a variable name.
  3. Default values for optional parameters.

Note that __UNKNOWN__ is a reserved parameter name. It specifies the type of undefined parameters (see below). If the XML-style parameters are provided, they are returned in an assotiative array (param name => value). In the OPT-style, the array has the numeric indices.

$style allows to force the parameter style we want to use. By default each instruction accepts both XML-style and position-based syntax. To force XML-style, set the value to OPT_STYLE_XML and to force the second variant, to OPT_STYLE_OPT.

Let's take a look at some examples:

Example 3.13. parametrize()

$params = array(
	'file' => array(OPT_PARAM_REQUIRED, OPT_PARAM_EXPRESSION),
	'default' => array(OPT_PARAM_OPTIONAL, OPT_PARAM_EXPRESSION, NULL),
	'assign' => array(OPT_PARAM_OPTIONAL, OPT_PARAM_ID, NULL),
	'__UNKNOWN__' => array(OPT_PARAM_OPTIONAL, OPT_PARAM_EXPRESSION, NULL)
);
$variables = $this -> compiler ->
	parametrize('include', $block -> getAttributes(), $params);

This is a part of real include instruction implemented in OPT. We see three "system" parameters: required file and optional default and assign. The two first parameters are parsed as expressions. We also allow to insert some additional parameters (__UNKNOWN__), which are returned to the $variables array. System parameter values are in the $params array. They are used by the instruction in this way:

Example 3.14. Using the parameters

foreach($variables as $name => $variable)
{
	$code .= ' $this -> vars[\''.$name.'\'] = '.$variable.'; ';		
}
// An optional parameter check
if($params['default'] != NULL)
{
	$code .= ' if(!$this -> doInclude('.$params['file'].')){
		$this -> doInclude('.$params['default'].'); } ';
}
else
{
	$code .= '$this -> doInclude('.$params['file'].');';
}