2.5. Functions

2.5.1. apply()
2.5.2. array()
2.5.3. capitalize()
2.5.4. count()
2.5.5. countWords()
2.5.6. cycle()
2.5.7. date()
2.5.8. length()
2.5.9. lower()
2.5.10. parseInt()
2.5.11. trim()
2.5.12. upper()
2.5.13. wordwrap()

A function is something that modifies the input data and generates the result. The input data are called parameters and they are simply some values we want to process. In OPT, functions have similar syntax to other programming languages and mathematics:

{function(parameters)}

parameters is a list of values or expressions separated by commas. If the function takes no parameters, we still keep the brackets. OPT has a number of different built-in functions and the programmers may create their own, too.

The example below shows the idea hidden behind the functions. Let's say the script sets the block called $introduction with a text "Welcome to our site", but in this layout, it has to be written with capitalized letters.

Example 2.8. Sample use of functions

<p>{upper($introduction)}</p>

Now the value of $introduction block is passed to the function, which capitalizes all the small letters and returns the modified text to the template.

Some functions appearing in OPT may be easily replaced by CSS stylesheets and in the HTML template design there is no need to use them. However, the template engine is intended to work not only with the HTML data, but can process any of the text content, where such features may be the only way, for example to capitalize the letters.

Below, we provided a complete reference of functions available in OPT. In the description, we use the following data types:

  1. Integer - a number that belongs to ..., -2, -1, 0, 1, 2 ...
  2. Float - a real number (with fractions), for example 3.14
  3. Number - integer or float
  4. String - any text
  5. Boolean - a logical value: 1 (true) or 0 (false).
  6. Array - a PHP array
  7. Object - a PHP object
  8. Lang - a language block
  9. Mixed - more than one types possible
  10. Void - the function returns no value
  11. ... - three dots mean a custom number of parameters.

Note: the square brackets in the synopsis mark optional parameters that do not have to be set, if not necessary.