2.3. Blocks and variables

A block is a place in the template, where you can put the results of your application. It starts with a dollar sign and can contain numbers, letters and underscores, just like PHP variables. The block may have both simple values, for example strings or numbers, and the complex types: arrays and objects. The block values are passed to the templates, using optClass::assign() method. This is a sample template that allows to put a dynamic date into the static text:

Example 2.2. Simple block

<p>Hello my friend, do you need {$thing}?</p>

Now, if the application programmer assigns the text "sunglasses" into the $thing block, the guest will see:

<p>Hello my friend, do you need sunglasses?</p>

Blocks can handle not only strings or numbers. They support also compound types known from PHP, like arrays and objects. An array is a set of variables grouped under the same name. For example, instead of writing $person_name, $person_surname, we use $person.name, $person.surname. You can say, it is almost the same, but the main difference is in the PHP code, where the data are more simply assigned to the templates. You should consult your PHP programmer, where you are going to use them.

Arrays bring also some more dynamics. For example, we have two of them: $titles and $values. They contain different kinds of data, and the titles for them, but we have another block: $id that describes, what record should be shown. OPT allows to do it:

Example 2.3. Blocks and arrays

<p><strong>{$titles[$what]}</strong>: {$values[$what]}</p>

This code tells: take the element from $titles, whose key (identifier) is in the $what block. The identifiers of array elements may be both numbers and strings. The full list of possible ways to access array data is shown below:

{$foo}           {* display a simple data (numbers, strings) *}
{$foo[4]         {* display the 5th element of a zero-indexed array *}
{$foo.bar}       {* display the "bar" key value of an array *}
{$foo[bar]}      {* display the "bar" key value of an array *}
{$foo.bar[pub]}  {* display the "pub" key of "bar" array in $foo block. *}
{$foo[$bar]}     {* display value of an array, whose name is in the $bar block *}

The notation with dots can be used, if we define the key statically. The notation with brackets allows to use dynamic keys, stored in some other blocks. Note also that we can mix those two notations in this way: firstly dot-based access, later brackets. The way, where the brackets are first and dots last, like $foo[bar].joe is not allowed.

Another compound type supported by OPT are objects. Unsually there is no need to use them in templates, although some programmers may find them important. An object is more than array. It contains not only the grouped data, but also the methods, which may perform some operations on them. The methods can take parameters, which are both static values, like "5" and "Mike", and other blocks. The code below shows, how to access the object data in OPT:

{$foo->bar}			{* the "bar" field of the object *}
{$foo->method()}			{* the method "method" of the $foo object called *}
{$foo->method(5)}			{* the method with a static parameter called *}
{$foo->method($bar)}		{* the method with a dynamic parameter $foo called *}
{$foo->method($abc, $def)}	{* the method with more than one parameters *}

Consult with the script programmer, which elements of the syntax you are going to use.

Block names can contain letters, numbers and underscores, where the first character must not be a number. OPT has only one reserved block name: $opt. It allows to access various information about different elements of the system. Note that the programmer may extend the field of use of this block, and here we will describe only the built-in options:

$opt.get.name - reads the "name" parameter from the URL address
$opt.post.name - reads the "name" field sent to the script by the HTML form
$opt.cookie.name - reads the "name" cookie value
$opt.session.name - reads the "name" value from the session data
$opt.server.name - reads the "name" value from the server information
$opt.env.name - reads the "name" value from the server information
$opt.request.name - reads the "name" value from the URL, the form or the cookie
$opt.now - returns the current time in seconds from 1.1.1970
$opt.const.name - returns the value of "name" PHP constant
$opt.version - returns the OPT version

Sometimes the template engine must set some values for the template designer, but as we said, it must not use blocks for this. All the data generated by OPT can be displayed, using the next syntax structure that is called variable. Its name begins with an at character, but it also can contain numbers, strings and compound types. The only difference is that it is set by the template engine, not the script. In the example below, we will define a variable inside a template and display it:

Example 2.4. Variables

{var=variable; "This is a variable"}
<p>{@variable}</p>

The first line defines a new variable. It will be explained later.

OPT allows to access directly its configuration, using configuration directives. Their names are followed by the hash character (#): {#root}.

Some websites may use multilingual interfaces. The messages displayed on the page are dynamically loaded depending on the selected language. OPT has a special kind of block, language blocks. They allow to read the texts directly from the language system used by the script. Each message is identified by the ID and belongs to a group. The language block looks like this: {$group@id}. The i18n system used by OPT is described later.