Web page Template Generation class
<?php
/*
Web page Template Generation class
Use: Mainly used for the generation of static resource surface pages. This class also applies to the generation of static pages for other systems
Invoke instance:
$TPL = new Templateengine ();
$tpl->opentemplate ("e:/t.htm");
$tpl->startelement = "<!--resource Title identification-Start-->";
$tpl->endelement = "<!--resource title identification-end-->";
$tpl->value = "This is the replacement content of the title of the resource";
$tpl->replacetemplate ();
$tpl->save ();
*/
The hidden contents of this post need to reply before you can browse
Class Templateengine
{
Public $filepath, $startelement, $endelement, $value;
Private $template, $fso, $fle, $regex, $filestate;
Function __construct ()
{
$this->filestate = false;
$this->filepath = ';
&nb Sp $this->template = ';
$this->startelement = ';
$this->endelement = ';
}
Private Function Filterstr ($str)
{
if ($str = = ' | | is_null ($STR)) return ';
// $str = s Tr_replace (', ' \ ', $str);
$STR = str_replace ('/', '/', $str);
$STR = Str_replace (' (, ', ', $str);
$STR = str_replace (') ', ') ', $str);
$STR = Str_replace (' * ', ' * ', $str);
$STR = Str_replace ('? ', '? ', $str);
$STR = Str_replace (' {', '} ', $str);
$STR = Str_replace ('} ', ' {', $str);
$STR = Str_replace ('. ', '. ', $str);
$STR = str_replace (' + ', ' + ', $str);
$STR = Str_replace (' [', ' [', $str];
$STR = Str_replace ('] ', '] ', $str);
return $str;
}
//Set template file path
Public function opentemplate ($mfilepath)
{
global $flib;
if (!) ( $this->template= @file_get_contents ($mfilepath)) {$flib->alert (' specified template file does not exist! ', ' back ', 0); exit ();}
$this->filestate = true;
$this->filepath = $mfilepath;
}
//Determine if the identity exists
Public Function iselement ()
{
if (! $this->filestate | | $this->template== ' || $this->startelement== ' | | $this->endelement== ') return;
$STRPATRN = "/". $this->filterstr ($this->startelement). " [ss]*?] $this->filterstr ($this->endelement). " /";
return Preg_match ($STRPATRN, $this->template)? 1:0;
}
//change template elements, element tags in general format: "<!-element tag-start--><!--element tag-end-->", you can also customize
//Element label is case-insensitive
Public Function replacetemplate ()
{
if (! $this->filestate | | $this->template== ' | | $this-> startelement== ' ' | | $this->endelement== ') return;
$STRPATRN = "/". $this->filterstr ($this->startelement). " [ss]*?] $this->filterstr ($this->endelement). " /";
$this->template = Preg_replace ($strpatrn, $this->startelement. $this->value. $this-> EndElement, $this->template);
}
///Save new template content
Public function Save ()
{
if (! $this->filestate) return;
@ File_put_contents ($this->filepath, $this->template);
}
//Save template Content
Public function saveas ($mfilepath)
{
if (! $this->filestate) return;
@ File_put_contents ($mfilepath, $this->template);
}
}
?