1.3. OPT API

OPT is designed to work with every kind of text data, not only XML documents, like some other engines. It is also designed to be as much extendable, as it's possible. Here comes the OPT API. It allows you to develop your own template parser using the native OPT template compiler. You find it useful, if you want to write an e-mailer class, which parser the e-mail templates. You do not need all those Gzip compressions, HTTP output caching, so you use just the template compiler and a simplified parser.

OPT API contains all the basic methods, like assign() or fetch(). Let's take a look at the sample script:

Example 1.3. OPT API usage

<?php 
	define('OPT_DIR', '../lib/');
	require('../lib/opt.api.php');
	
	class optParser extends optApi
	{
		// Your code goes here
	}

	try{ 
		$tpl = new optParser; 
		$tpl -> root = './templates/';
		$tpl -> compile = './templates_c/';
		$tpl -> assign('current_date', date('d.m.Y'));
		$tpl -> parse('my_template.tpl');
	}
	catch(optException $exception)
	{ 
		optErrorHandler($exception); 
	}
?>				

The compile directive is required by the compiler, so even such APIs should provide it for the programmer. Runtime compilation is much slower than executing precompiled templates.