I love templates but I will also see how does it looking befor I use it. I know genshi from python a very nice xhtml template system with includes & co. For PHP I found an also nice (but without include command) TAL based system: PHPTAL (http://phptal.org/). It is i18n'able (translations) and replaces contents and attributes while running app. (Someone ca develop a template, you extends it by setting any tal: attributes. He/she will see the basic design, you and the visitors the real site. Example "<a href='site2.html' tal:attributes='href string:${domainname}/site2.html' tal:replace="${link_name_from_controller}">site2</a>"). An other thing is, that the cache can be set per file or attribute! But it can also compile every time (in "dev" mode).
I have now an controller TalController.php to use PHPTAL:
- Code: Select all
<?php
/**
* Controller to use PHPTAL, where PHPTAL must be in app/protected/class
* @author UmbertoKo
* @license GPL v3
*/
class TalController extends DooController
{
private $tpl;
private $print_output = true;
private $templatepath = '';
public $__taldebug = array();
function __construct($templatename, $returnmode = true, $outputMode = 0, $encoding = 'UTF-8', $lifetime = 1)
{
require_once Doo::conf()->SITE_PATH.'/protected/class/PHPTAL.php';
$this->templatepath = Doo::conf()->SITE_PATH.'protected/view/'.$templatename.'.html';
$this->print_output = $returnmode;
if($outputMode == 0)
{
$outputMode = PHPTAL::HTML5;
}
try
{
$this->tpl = new PHPTAL($this->templatepath);
$this->tpl->setOutputMode($outputMode);
$this->tpl->setEncoding($encoding);
$this->tpl->setTemplateRepository(Doo::conf()->SITE_PATH.'protected/view/');
$this->tpl->setPhpCodeDestination(Doo::conf()->SITE_PATH.'protected/viewc/');
$this->tpl->setCacheLifetime($lifetime);
if(Doo::conf()->DEBUG_ENABLED)
{
$this->tpl->setForceReparse(true);
}
if(Doo::conf()->APP_MODE == 'dev' || Doo::conf()->DEBUG_ENABLED)
{
$this->__taldebug[] = var_export($this->tpl, 1);
}
}
catch (Exception $e)
{
echo $e;
}
}
public function setVars($vars)
{
try
{
foreach($vars as $i => $dat)
{
$this->tpl->$i = $dat;
}
if(Doo::conf()->DEBUG_ENABLED)
{
$this->__taldebug[] = var_export($this->tpl, 1);
}
}
catch (Exception $e)
{
echo $e;
}
}
public function execute()
{
if(Doo::conf()->DEBUG_ENABLED)
{
$this->tpl->tal_debug_messages = stripslashes(var_export($this->__taldebug, 1));
}
$output = $this->tpl->execute();
if(Doo::conf()->DEBUG_ENABLED)
{
$this->__taldebug[] = var_export($this->tpl, 1);
}
if($this->print_output == true)
{
echo $output;
return;
}
return $output;
}
}
?>
I use it in this way (from an other controller):
- Code: Select all
require_once(dirname(__FILE__).'/TalController.php');
$tpl = new TalController('template_new');
$body = new TalController('body_template', false);
$body->setVars(self::$data);
self::$data['inc'] = $body->execute();
unset($body);
$tpl->setVars(self::$data);
$tpl->execute();
$tpl is the fist template object, which make "print out" (not return if the 2nd param in constructor is true [default]). execute() runs "echo".
$body ist the 2nd template object to render only a sub-template. It will be returned to a var ('inc'). inc is a part of the main template ('template_new') and will be replaced with this value:
- Code: Select all
<div tal:condition="inc">
${structure inc}
</div>
Both templates 'body_template.html' and 'template_new.html' must be a valid XHTML and be placed in app/protected/view/.
I don't know if this is the best way, but it works fine. To use is save TalController class in app/protected/controller/ and copy the content of http://phptal.org/files/PHPTAL-1.2.1.zip (content of PHPTAL-1.2.1 folder but not itself) to app/protected/class/. And do not forget "require_once(dirname(__FILE__).'/TalController.php');".
Will know, what do you mean.
UmbertoKo
BTW: Do we have a plugin repository? Or a standard way to commit some?
