| (use to parse documents)
V
+--------------+
|StandardParser|
+--------------+
|
|
--
V
+----------------+
| TemplateParser |
+----------------+
<>
|
| (use to parse each tags)
|
V
+----------+ +----------+ +-------------+
| TagBasis |<|---| DataTag |---------| ConcreteTags|
+----------+ | ArrayTag | +-------------+
+----------+ tag_val,tag_each,,,,etc.
*/
define("FILE_READ_UNIT",8192);
/*
* Tag definition
*/
/* the origine class of all tags */
class TagBasis{
var $matchregexp;
var $fromstring;
var $tostring;
var $closestring;
var $regardasarray=false;
function getLabelArray($str){
$ans=array();
preg_match_all($this->matchregexp,$str,$regans,PREG_SET_ORDER);
foreach($regans as $x){
$ans[]=$x[1];
}
return $ans;
}
function parse($str,$multilabels){
while(preg_match($this->matchregexp,$str,$match)){
//未定義エラー対策
if(isset($match[1])){
$ind=$this->getIndex($match[1],$multilabels);
}
else{
$ind=$this->getIndex("",$multilabels);
}
$str=str_replace(
call_user_func_array("sprintf",array_merge(array($this->fromstring),array_slice($match,1))),
call_user_func_array("sprintf",array_merge(array($this->tostring),array($ind),array_slice($match,1))),
$str);
}
$str=$this->closeTag($str);
return $str;
}
function closeTag($str){
if($this->closestring){
$str=str_replace($this->closestring,
"",
$str);
}
return $str;
}
function getIndex($m,$multilabels){}
}
/* the super class of tags which handle non-array data */
class DataTag extends TagBasis{
function getIndex($m,$multilabels){
$ar=explode("/",$m);
$ind="";
$rui=array();
foreach($ar as $x){
array_push($rui,$x);
$ind.="[\"$x\"]";
if(in_array(join("/",$rui),$multilabels)){
$ind.="[\$cnt[\"".join("/",$rui)."\"]]";
}
}
return $ind;
}
}
/* the super class of tags which handle array structure
like {each *} */
class ArrayTag extends TagBasis{
function getIndex($m,$multilabels){
$ar=explode("/",$m);
$ind="";
$rui=array();
$mattan=0;
foreach($ar as $x){
array_push($rui,$x);
$ind.="[\"$x\"]";
if($mattan!=count($ar)-1 && in_array(join("/",$rui),$multilabels)) {
$ind.="[\$cnt[\"".join("/",$rui)."\"]]";
}
$mattan++;
}
return $ind;
}
}
/*
* parser classes
*/
/* main definition of parser */
class TemplateParser{
var $tags=array("simple"=>array(),"multi"=>array());
function add($tag){
if($tag->regardasarray) $this->tags["multi"][]=$tag;
else $this->tags["simple"][]=$tag;
return $this;
}
function remove($tagname){
$this->_remove($tagname,$this->tags["multi"]);
$this->_remove($tagname,$this->tags["simple"]);
}
function clear(){
$this->tags=array("simple"=>array(),"multi"=>array());
}
function _remove($tagname,&$tags){
for($i=0;$itags["multi"]);
$multilabels=array();
foreach($this->tags["multi"] as $x){
$multilabels=array_merge($multilabels,$x->getLabelArray($str));
}
reset($this->tags["multi"]);
foreach($this->tags["multi"] as $x){
$str=$x->parse($str,$multilabels);
}
reset($this->tags["simple"]);
foreach($this->tags["simple"] as $x){
$str=$x->parse($str,$multilabels);
}
return $str;
}
}
////////////////////////////////////////////////////
/*
* Standard tag classes
* these tags are defined as previous version of htmltemplate
*/
class tag_val extends DataTag{
var $matchregexp='/\{val ([^\}]+)\}/i';
var $fromstring="{val %s}";
var $tostring="\n";
}
class tag_rval extends DataTag{
var $matchregexp='/\{rval ([^\}]+)\}/i';
var $fromstring="{rval %s}";
var $tostring="\n";
}
// contributed by Osamu Shigematsu
class tag_def extends ArrayTag{
var $matchregexp='//i';
var $fromstring="";
var $tostring="";
var $closestring="";
}
//contributed by Ahiru,FB,masao,,,,
class tag_ndef extends ArrayTag{
var $matchregexp='//i';
var $fromstring="";
var $tostring="";
var $closestring="";
}
// this vdef is an experimental tag.
class tag_vdef extends DataTag{
var $matchregexp='//i';
var $fromstring="";
var $tostring="0))){ ?>";
var $closestring="";
var $regardasarray=1;
}
class tag_each extends ArrayTag{
var $matchregexp='//i';
var $fromstring="";
var $tostring="";
var $closestring="";
var $regardasarray=true;
}
/* ***
* comment
* remove all characters between these tags.
*/
class tag_comment extends ArrayTag{
var $matchregexp='//i';
var $fromstring="";
var $tostring="";
var $closestring="";
}
/*
* StandardParser
* parser defined with above tags.
* behave as previous htmltemplate
*/
class StandardParser extends TemplateParser{
// function StandardParser(){
function __construct(){
$this->add(new tag_val());
$this->add(new tag_rval());
$this->add(new tag_def());
$this->add(new tag_ndef());
$this->add(new tag_vdef());
$this->add(new tag_each());
$this->add(new tag_comment());
}
}
/*
* htmltemplate
* the APIs defined after the manner of htmltemplate for PHP4
* tmp file generation has not been implemented yet.(2003-07-08)
*/
class htmltemplate{
private $parser;
// the constructor
// private function htmltemplate(){
private function __construct(){
$this->parser=new StandardParser();
}
// the static method to get the singleton instance
//ここから下のfunctionにpublic staticを追加しstatic宣言。
//PHP7のE_DEPRECATEDの非推奨のエラーはでなくなったが
//PHP4の文法でインスタンスを使用しているので
//可能であれば修正したほうが良い。
//PHP マニュアル
//http://jp2.php.net/manual/ja/language.oop5.static.php
//2018.09.22
private static function &getInstance(){
static $instance;
if(! $instance) $instance=new htmltemplate();
return $instance;
}
// adds tag instance
public static function add($tag){
// This method is obsolete.Use addTag() instead.
$inst=&htmltemplate::getInstance();
$inst->parser->add($tag);
}
// adds a tag class
public static function addTag($tagclassname){
$inst=&htmltemplate::getInstance();
$inst->parser->add(new $tagclassname());
}
// removes a tag class
public static function removeTag($tagclassname){
$inst=&htmltemplate::getInstance();
$inst->parser->remove($tagclassname);
}
// remove all tags
public static function removeAllTag(){
$inst=&htmltemplate::getInstance();
$inst->parser->clear();
}
// sets a parser instance
public static function setParser($parser){
$inst=&htmltemplate::getInstance();
$inst->parser=$parser;
}
// parses given template string
public static function parse($str){
$inst=&htmltemplate::getInstance();
return $inst->parser->parse($str);
}
// parses a template file and displays it
public static function t_include($file,&$data){
$inst=&htmltemplate::getInstance();
print $inst->t_buffer($file,$data);
}
// parses a template file and gives it back
public static function t_buffer($file,&$data){
$val=&$data;
$inst=&htmltemplate::getInstance();
$handle=fopen($file,"rb");
$all="";
do {
$bytedata = fread($handle, FILE_READ_UNIT);
if (strlen($bytedata) == 0) {
break;
}
$all .= $bytedata;
} while(true);
return $inst->buffer($all,$val);
}
public static function buffer($template_str,&$val){
$inst=&htmltemplate::getInstance();
$code=$inst->parse($template_str);
ob_start();
eval('?>' .$code);
$cnt=ob_get_contents();
ob_end_clean();
return $cnt;
}
}
?>