1.1. Simple installation

A typical installation process is done in two stages. First, copying files. The project contains several PHP scripts named opt.filename.php. These files need to be copied into your project directory tree, wherever you want. You may create a separate directory for them, or mix with other core files. It is recommended to make the directory unavailable from the browser level. Then you have to make two directories for the templates: templates and templates_c. The former contains all the templates we use in our website. The latter is a kind of cache for precompiled templates generated by OPT.

Secondly, the script building:

  1. Set the OPT_DIR constant containing the path to the library files. OPT may create it on its own, if the files are in the current directory. We do not recommend to put here null value:
    define('OPT_DIR', '');
    This may cause the application gets slower - PHP has to check every predefined path.
  2. Include opt.class.php file containing the main library class.
  3. Place the rest of the code inside try{ ... } catch(){ ... } block. OPT reports the errors using PHP exception system and we need it to catch them. If you do not want to write your own exception handler, you may use the standard one placed in optErrorHandler() function.
  4. Create optClass object.
  5. Create a configuration. You may set the directives manually (at least root and compile containing paths to the templates and their compiled versions) or load them using loadConfig() method.
  6. Optionally you may send HTTP headers using httpHeaders() method.

Now your script is ready to work. A sample application looks like this:

Example 1.1. Sample OPT script

<?php 
  // set the path to the library 
  define('OPT_DIR', '../lib/'); 
  // load the library 
  require(OPT_DIR.'opt.class.php'); 
  try{  
    // create parser object 
    $tpl = new optClass; 
    // configure OPT 
    $tpl -> root = './templates/'; 
    $tpl -> compile = './templates_c/'; 
    $tpl -> gzipCompression = 1; 
    // send headers 
    $tpl -> httpHeaders(OPT_HTML);  
  
    // process the template 
    $tpl -> assign('current_date', date('d.m.Y, H:i'));  
    $tpl -> parse('example1.tpl');  
  }catch(optException $exception){  
    optErrorHandler($exception);  
  }  
?>				

Notice, how we configure the library. We just set the fields inside the class. root is the path to the location, where the templates are stored. compile is the second location we have created. Here OPT will save precompiled templates. Third directive, gzipCompression tells that the output should be compressed, if the browser supports such option.

The configuration may be also stored as array:

$config = array(
	'root' => './templates/',
	'compile' => './templates_c/',
	'gzipCompression' => 1
);
$tpl -> loadConfig($config);

Or even a configuration file:

; <?php die(); ?>
; OPT configuration
root = "./templates/"
compile = "./templates_c/"
gzipCompression = 1

It may be loaded in this way:

$tpl -> loadConfig('my_config.php');