5.1. New functions

Creating of new functions is really easy in OPT. Let's begin with the PHP code. The function we want to use in the templates, must have the name started with the opt prefix, and get the optClass instance as the first parameter. The function must process the data and return a value.

Example 5.1. New OPT function

<?php
								  
	function optPower(optClass $tpl, $x, $y = 2)
	{
		// return the value
		return pow($x, $y);
	} // end optPower();
							  
?>

Then, we have to register our function in OPT. There are three ways of doing it:

  1. Using plugins (described in chapter OPT Plugins).
  2. In the constructor, if we extend optClass class.
  3. Using optClass::registerFunction() method.

Beware! In all these methods we specify the function name without the opt prefix!

$tpl -> registerFunction('pow', 'Power');

The first parameter defines the function name used in the templates, the second - PHP function name withour prefix. The use of the function:

<p>The second power of 3 is {pow(3)}.</p>