Ad. OPT2 -> at this point, I assume that about 80% of the library is completed and the rest will be added soon. But this is only a development version and it was not tested on the real website yet. Once the code is completed, there will be a beta release, then at least one Release Candidate and at last, the final version. I work as quick as possible, because we need this library, too :).
Despite the library, I write the documentation at the same time, but it is a bit delayed. Currently, I write only the Polish version and it will be translated into English later (but still before the final release).
But going back to the topic... I've done some research and added
compileId to my local code. Moreover, I found a small bug in a rarely used feature :). In the weekend, I'll release OPT 1.1.4 with these updates, because I have to do some necessary tests and update the manual.
BTW. I have these files from Zend Framework + OPT connection. My code is not complicated:
bootstrap.php<?php
define('OPT_DIR', BOOT_LOCATION.'library/ext/opt/');
define('OPF_DIR', BOOT_LOCATION.'library/ext/opf/');
define('DIR_TPLCC', BOOT_LOCATION.BOOT_APPNAME.'/data/tpl_compile/');
define('DIR_TPL_CACHE', BOOT_LOCATION.BOOT_APPNAME.'/data/tpl_cache/');
// I include the library manually.
require(OPT_DIR.'opt.class.php');
require(OPF_DIR.'opf.class.php');
...
// OPT
$view = new My_View;
$view -> setScriptPath(NULL);
$view -> setI18n($translate);
Zend_Registry::set('view', $view);
...
?>
My/View.php<?php
class My_View implements Zend_View_Interface
{
const ERROR = 0;
const INFO = 1;
private $_tpl;
private $templates;
private $renderer = NULL;
private $overall = true;
private $title;
private $navigation = array();
public function __construct()
{
$config = Zend_Registry::get('config');
$this -> _tpl = new optClass;
$this -> _tpl -> compile = DIR_TPLCC;
$this -> _tpl -> cache = DIR_TPL_CACHE;
$this -> _tpl -> xmlsyntaxMode = true;
$this -> _tpl -> gzipCompression = false;
$this -> templates = array();
} // end __construct();
public function getEngine()
{
return $this -> _tpl;
} // end getEngine();
public function setScriptPath($name)
{
if(is_null($name))
{
$this -> _tpl -> root = BOOT_LOCATION.BOOT_APPNAME.'/views/';
$this -> _tpl -> compile = DIR_TPLCC;
}
else
{
$this -> _tpl -> root = BOOT_LOCATION.BOOT_APPNAME.'/views/'.$name.'/';
$this -> _tpl -> compile = DIR_TPLCC.$name.'/';
}
} // end setScriptPath();
public function __set($key, $value)
{
$this -> _tpl -> assign($key, $value);
} // end __set();
public function __get($key)
{
return $this -> _tpl -> data[$key];
} // end __get();
public function __isset($key)
{
return isset($this -> _tpl -> data[$key]);
} // end __isset();
public function __unset($key)
{
unset($this -> _tpl -> data[$key]);
} // end __unset();
public function assign($name, $value = NULL)
{
$this -> _tpl -> assign($name, $value);
} // end assign();
public function clearVars()
{
$this -> _tpl -> data = array();
} // end clearVars();
public function render($name)
{
$this -> templates = array(0 => $name);
$session = Zend_Registry::get('session');
$session -> save();
$this -> display();
die();
} // end render();
public function setI18n(ioptI18n $i18n)
{
$this -> _tpl -> setObjectI18n($i18n);
} // end setI18n();
public function addNavigation($name, $address)
{
$this -> navigation[] = array('name' => $name, 'address' => $address);
} // end addNavigation();
public function addPages($pages)
{
$translate = Zend_Registry::get('translate');
$this -> _tpl -> assign('which', $translate -> putApply('global', 'pageStats', $pages->getActive(), $pages->getSize()));
$this -> _tpl -> assign('pages', $pages);
} // end addPages();
public function systemPrepare()
{
$data = Zend_Registry::get('data');
if(!is_null($this -> renderer))
{
$renderer = My::loadRenderer($this -> renderer);
}
else
{
$renderer = My::loadRenderer($data->domain['hash']);
}
$renderer -> systemPrepare($this);
$this -> renderer = $renderer;
} // end systemPrepare();
public function display()
{
$config = Zend_Registry::get('config');
$data = Zend_Registry::get('data');
$this -> _tpl -> charset = 'utf-8';
if($config -> terenzzia -> debug)
{
$this -> _tpl -> httpHeaders(OPT_HTML);
}
else
{
$this -> _tpl -> httpHeaders(OPT_HTML);
}
$this -> renderer -> prepare($this);
$this -> _tpl -> assign('address', $config -> website -> address);
$this -> _tpl -> assign('media', $config -> website -> media);
$this -> _tpl -> assign('navigation', $this -> navigation);
$this -> _tpl -> assign('title', $this -> title.' - '.$data->domain['title']);
$session = Zend_Registry::get('session');
$session -> save();
$this -> _tpl -> setMasterTemplate('master.tpl');
if($this -> overall)
{
$this -> _tpl -> parse('overall_header.tpl');
}
foreach($this -> templates as $tpl)
{
$this -> _tpl -> parse($tpl);
}
if($this -> overall)
{
$this -> _tpl -> parse('overall_footer.tpl');
}
} // end display();
public function setTemplate($tpl)
{
$this -> templates[] = $tpl;
} // end setTemplate();
public function setTitle($title)
{
$this -> title = $title;
} // end setTitle();
public function setOverall($overall)
{
$this -> overall = $overall;
} // end setOverall();
public function setRenderer($renderer)
{
$this -> renderer = $renderer;
} // end setRenderer();
public function message($type, $message, $redirect = '')
{
if($type == Terenzzia_View::ERROR)
{
$this -> error = true;
}
else
{
$this -> error = false;
}
$this -> message = $message;
$this -> redirect = $redirect;
$this -> render('message.tpl');
} // end message();
public function question($message, $redirect)
{
$opf = Zend_Registry::get('opf');
$val = Zend_Registry::get('validator');
if($val -> map('ans', OPF_GET, new opfStandardContainer(
new opfConstraint(MAP_TYPE, TYPE_STRING),
new opfConstraint(MAP_MATCHTO, '/yes|no/')
)))
{
if($val -> ans == 'yes')
{
return true;
}
return false;
}
$router = Zend_Registry::get('router');
$this -> message = $message;
$redirect['ans'] = 'yes';
$this -> urlYes = $router -> createURL($redirect);
$redirect['ans'] = 'no';
$this -> urlNo = $router -> createURL($redirect);
$this -> render('question.tpl');
} // end question();
} // end My_View;
?>
As you see, all the path-related issues are done in one method:
setScriptPath(). Writing your own handler for OPT is not necessary (in fact, the basic thing you have to do is to load the library and create an object), but it helps in some issues. All the paths are defined in
bootstrap.php file as constants, so that it's easy to manage them.