OPT API is a simplified version of the main OPT parser. It contains only the
most important methods and fields necessary to run the compiler. OPT API is the
best solution, if you want to use OPT as a parser in your e-mailer class or
similar. The optApi
class contains the following methods
and fields from the main parser:
In order to use OPT API, load the opt.api.php
file into
your script. You can keep both the main parser and the API loaded - just remember
to include the opt.api.php
after you
load the opt.class.php
file.
By default, OPT API is null - there are no instructions or functions registered. It is you, who decides, what the parser can. The API provides a method only for instruction adding, because it is a bit complicated task. To add other features, you fill the following class fields manually:
Note that OPT API does not have custom resource support.
Although OPT API provides some simple template parsing methods, you can freely write new ones, if you need it. To load and (optionally) compile a template, the parsing method must contain the following code:
Example 5.12. Writing custom parsing methods
public function customParse($tpl) { $compiled = $this -> needCompile($tpl); // 1 $oldErrorReporting = error_reporting(E_ALL ^ E_NOTICE); // 2 include($this -> compile.$compiled); error_reporting($oldErrorReporting); } // end customParse();
To capture the generated output, OPT uses ob_start() and ob_get_clean() methods.
To explain, how to control the compiler manually, we are going to describe the needCompile() method:
Example 5.13. Controlling the compiler
protected function needCompile($filename, $noException = false) { $compiled = optCompileFilename($filename); // 1 $compiledTime = @filemtime($this -> compile.$compiled); // 2 $result = false; $rootTime = @filemtime($this -> root.$filename); if($rootTime === false) { // 3 if($noException) { return NULL; } $this -> error(E_USER_ERROR, '"'.$filename.'" not found in '.$this->root.' directory.', OPT_E_FILE_NOT_FOUND); } if($compiledTime === false || $compiledTime < $rootTime || $this -> alwaysRebuild) { // 4 $result = file_get_contents($this -> root.$filename); } if($result === false) { // 5 return $compiled; } if(!is_object($this -> compiler)) { // 6 require_once(OPT_DIR.'opt.compiler.php'); $this -> compiler = new optCompiler($this); } $this -> compiler -> parse($this -> compile.$compiled, $result); // 7 return $compiled; // 8 } // end needCompile();
As you see in the listing, OPT API does not support the performance directive.