Ha, I haven't written a blog for a long time. I can just write it today. why is this blog editor FCK? I wrote a very simple template class for inspiration. although there is nothing advanced, I always think that inspiration also needs to be accumulated. Success
Ha, I haven't written a blog for a long time. I can just write it today. why is this blog editor FCK?
I wrote a very simple template class for inspiration. although there is nothing advanced, I always think that inspiration also needs to be accumulated.
It's a natural success. Template File Code:
/**
* Author: darasion
* Copyright: indicate the author in the application or reprinted form.
*
*/
Class DarasionTemplate {
Var $ template = '';
Var $ VAR = array ();
Var $ className = 'darasiontemplate ';
/**
* Set parameters/templates
*
*/
Function setVar ($ name, $ value ){
If (strtolower (get_class ($ value) = strtolower ($ this-> className )){
$ This-> VAR [$ name] = $ value-> parse ();
} Else {
$ This-> VAR [$ name] = $ value;
}
}
/**
* Get the parameter/parsed Template
*
*/
Function getVar ($ name ){
If (isset ($ this-> VAR [$ name]) {
Return $ this-> VAR [$ name];
}
}
/**
* Set the template path
*
*/
Function setTemplate ($ tpl ){
$ This-> template = $ tpl;
}
/**
* Output html
*
*/
Function out (){
Echo $ this-> parse ();
}
/**
* Parsing template
*
*/
Function parse (){
Ob_start ();
Include_once ($ this-> template );
Echo $ content = ob_get_contents ();
Ob_end_clean ();
Return $ content;
}
}
?>
This template class supports template nesting. you only need to set the instance of the subtemplate class as a parameter to the parent template.
Application method:
Test. php
Include ('darasiontemplate. php ');
// Create a parent template
$ Tpl = new DarasionTemplate ();
$ Tpl-> setTemplate ('Inc/_ tpl. php ');
// Parent template parameters
$ Tpl-> setVar ('title', 'title' parameter ');
$ Tpl-> setVar ('A', 'parameter ');
$ Tpl-> setVar ('B', 'parameter B ');
// Create a subtemplate
$ Tpl1 = new DarasionTemplate ();
$ Tpl1-> setTemplate ('Inc/_ tpl1.php ');
// Set the subtemplate parameters
$ Tpl1-> setVar ('KK ', 'KK ');
// Add the subtemplate to the parent template
$ Tpl-> setVar ('C', $ tpl1 );
$ Tpl-> out ();
?>
// Parent template :__ tpl. php
Http://www.w3.org/TR/html4/loose.dtd'>
<? Php echo @ $ this-> getVar ('title');?>
GetVar ('A');?>
GetVar ('B');?>