4. Programmer's Guide
4.1. Initialization
4. Programmer's Guide
« Previous
4.1.1. List of configuration options
Next »

4.1. Initialization

Table of Contents

The first thing we have to do is to initialize and configure our script in order to use OPT.

Autoloader

We start with setting up the autoloader which must be included manually. You can find it in Opl/Base.php file - it is a part of the OPL core:

<?php
require('../vendor/opl/Opl/Base.php');
Opl_Loader::setDirectory('../vendor/opl/');
Opl_Loader::register();

OPT does not force you to use include_path. With Opl_Loader::setDirectory() method, we specify the path to the OPL library directories. Finally, we register the autoloader in PHP. The OPL core provides a very flexible mechanism of loading the files that allows you to choose different locations for each library and even a file:

<?php
require('../vendor/Opl/Base.php');
Opl_Loader::mapLibrary('Opl', '../vendor/Opl/');
Opl_Loader::mapLibrary('Opt', '../anotherDirectory/Opt/');
Opl_Loader::register();

While this feature is rather useless in most of the script, as we usually want to keep similar pieces of the code in the same place, we might find it necessary if we wish to use PHAR-s.

Once we have registered the autoloader, we might also configure the options that affect all the OPL libraries:

Opl_Registry::setState('opl_debug_console', true);
Opl_Registry::setState('opl_extended_errors', true);

The first line enables the debug console. In this mode, each HTTP request will generate an extra pop-up window providing some information about OPT, like the executed templates, their execution time, the current configuration or estimated memory usage during the template compilation. The second option is very useful in the development environment, because it causes the default exception handler to show all the possible information about the exception and system state that helps to find the problem.

Do not forget to disable those two options on the production server!

Creating the main OPT class

Once our script knows, how to load OPL code, we are going to initialize Open Power Template. It is also a quite easy task. All we have to do is to create an object of Opt_Class class and set some configuration directives. They are available as public class variables:

$tpl = new Opt_Class;
$tpl->sourceDir = './templates/';
$tpl->compileDir = './templates_c/';

The complete list of configuration options can be found in List of configuration options. Note that you can also load the configuration from an array or INI file with loadConfig() method:

$tpl = new Opt_Class;
 
// Loading from INI file.
$tpl->loadConfig('./opt.ini');
 
// Loading from array
$cfg = array(
   'sourceDir' => './templates/',
   'compileDir' => './templates_c/'
);
$tpl->loadConfig($cfg);

The external configuration source does not have to set all the directives. Feel free to mix both of the configuration sources in one script.

Registering additional stuff

Although OPT comes bundled with a wide variety of different template instructions and functions, you will probably want to write custom system-specific ones. This is the right place to do it. You can either use plugins (see Plugins) or register the new stuff manually using Opt_Class::register() method. The details, how to register different items can be found in Extending OPT.

Finalizing the initialization

To finish the initialization, we call:

$tpl->setup();

This method is quite necessary, because it must prepare some data in order to make the rest of the script fully functional. Never forget to call it!

4.1. Initialization
4. Programmer's Guide
« Previous
4. Programmer's Guide
Next »
4.1.1. List of configuration options