<?php
/* This class extends phplibs template.inc Template class
- automatic block detection / declaration including nested blocks
- auto blanks unused blocks
- allows specification of template ihtml file, template path, handle in one line instead of many
to get phplib template class go to
https://sourceforge.net/projects/phplib/
- thanks to phplib for such an excellent template class :)
*/
class AppTemplate extends Template {
var $At;
var $Aname;
var $Afile;
var $Apath;
function AppTemplate($name, $file, $path){
$this->Aname=$name;
$this->Afile=$file;
$this->Apath=$path;
$this->Template($this->Apath);
$this->set_file($this->Aname, $this->Afile );
$this->findBlocks();
$this->parse("output", $this->Aname);
}
var $Aihtml="";
var $fd;
var $Ablocks;
var $blocklist = array();
function findBlocks(){
/* // USE THIS COMMENTED CODE ON SYSTEMS WITHOUT AWK AVAILABLE WHERE SPEED IS NOT A MAJOR CONCERN
//THE AWK SEARCH IS MUCH FASTER
if(!$this->fd = fopen($this->Apath."/".$this->Afile, "r")){
echo "Failed opening template";
die();
}
while(!feof($this->fd)){
$this->Aihtml .= fgets($this->fd, 1046);
}
preg_match_all("/<\!\-\- BEGIN (.*) \-\->/", $this->Aihtml, $this->blocklist, PREG_PATTERN_ORDER);
$this->Ablocks['begin'] = $this->blocklist[1];
preg_match_all("/<\!\-\- END (.*) \-\->/", $this->Aihtml, $this->blocklist, PREG_PATTERN_ORDER);
$this->Ablocks['end'] = $this->blocklist[1];
*/
//get the end block names
$command = "awk '/END/{print $3}' $this->Apath/$this->Afile";
$list=`$command`;
$ends = explode("\n", trim($list));
foreach($ends as $end){
if(strlen($end)>0){
$this->Ablocks['end'][] = $end;
}
}
//get the begin block names
$command = "awk '/BEGIN/{print $3}' $this->Apath/$this->Afile";
$list=`$command`;
$ends = explode("\n", trim($list));
foreach($ends as $end){
if(strlen($end)>0){
$this->Ablocks['begin'][] = $end;
}
}
$this->setupBlocks();
}
var $b;
function setupBlocks(){
//set them in the end order to retain nested blocks - blank them after creating -to hide unused blocks
foreach($this->Ablocks['end'] as $this->b){
$this->set_block($this->Aname, $this->b, $this->b."_block");
$this->set_var($this->b."_block", "");
}
}
var $Ablock_name;
var $AvarArray = array();
var $key; var $val;
function AparseBlock($block_name, $varArray){
$this->Ablock_name = $block_name;
$this->AvarArray = $varArray;
foreach($this->AvarArray as $this->key=>$this->val){
$this->At->set_var($this->key, $this->val);
$this->At->parse($this->Ablock_name."_block", $this->Ablock_name, true);
}
}
function pp(){
$this->pparse("output", $this->Aname);
}
}
?>