Ha, for a long time do not write a blog, today casually write a point, how this blog editor has become a FCK?
Sudden inspiration, wrote a very simple template class, although there is no advanced things, but I always think that inspiration also need to accumulate.
Kong. Template class file code:
<?php
/**
* Author: darasion
* Copyright: Use or reprint to indicate the author
*
*/
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/parse template
*
*/
function GetVar ($name) {
if (Isset ($this->var[$name])) {
return $this->var[$name];
}
}
/**
* Set Template path
*
*/
function SetTemplate ($TPL) {
$this->template = $TPL;
}
/**
* Output HTML
*
*/
function out () {
echo $this->parse ();
}
/**
* Parse Template
*
*/
Function Parse () {
Ob_start ();
Include_once ($this->template);
echo $content =ob_get_contents ();
Ob_end_clean ();
return $content;
}
}
?>
This template class can support the nesting of templates, as long as the instance of the child template class is set to the parent template as a parameter
How to use:
test.php
<?php
Include ("darasiontemplate.php");
Create a parent template
$TPL =new darasiontemplate ();
$tpl->settemplate ("inc/__tpl.php");
Parent Template Parameters
$tpl->setvar ("title", "parameter title");
$tpl->setvar ("A", "parameter A");
$tpl->setvar ("B", "parameter B");
Create a child template
$TPL 1=new darasiontemplate ();
$tpl 1->settemplate ("inc/__tpl1.php");
To set a child template parameter
$tpl 1->setvar ("KK", "KK");
Put a child template in a parent template
$tpl->setvar ("C", $tpl 1);
$tpl->out ();
?>
Parent stencil: __tpl.php
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title><?php echo @ $this->getvar ("title");? ></title>
<body>
<?php echo @ $this->getvar ("a");? >
<?php echo @ $this->getvar ("B");? >
<?php echo @ $this->getvar ("C");? >
</body>
Sub-Template: __tpl1.php
<table width= border= "1" bordercolor= "#000000" >
<tr>
<td><?php echo @ $this->getvar ("KK");? ></td>
<td><?php echo @ $this->getvar ("KK");? ></td>
<td><?php echo @ $this->getvar ("KK");? ></td>
</tr>
</table>
HTML for output:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "http://www.w3.org/TR/html4/loose.dtd" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> parameter title</title>
<body>
Parameter a parameter b
<table width= border= "1" bordercolor= "#000000" >
<tr>
<td>kk</td>
<td>kk</td>
<td>kk</td>
</tr>
</table>
</body>
In this template class, you use a different method than the other template classes to implement:
1, the other template class similar to {a} replaced by the <?php echo @ $this->getvar ("a"); > and their form is the same as the child template and the parent template.
2, if you need to loop, just write the PHP loop code, such as: foreach ().
The benefits of doing this are:
1, omitted the replacement of the string process (a large pile of regular is always do not understand, hehe, gave himself a lazy way to think).
2, the process of learning a lot of things that labels are not tags is omitted (or because of laziness).
3, the more important advantage is that this template class corresponding to the template, is fully available with Dreamweaver and other tools for visual editing.
Because it is a temporary rise of something written, it will certainly not withstand careful scrutiny, but for everyone to put forward an idea for everyone to discuss, if it can really help you in practice, naturally better.