Very simple php template class-DarasionTemplate

Source: Internet
Author: User
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');?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.