Building infrastructure
Create under Project folder (custom)
(1) Core catalogue: WQ
(2) Template catalog: Moban
(3) Compile directory: Bianyi
(4) Create profile: config.ini.php
<?PHP//get the directory where the files are located$Gen=dirname(__file__);//set up a template directoryDefine(' Moban ',$Gen.‘ /moban/');//set the core class directoryDefine(' WQ ',$Gen.‘ /wq/');//Set compilation file directoryDefine(' Bianyi ',$Gen.‘ /bianyi/');//load Core classincludeWQ. ' WQ.class.php ';//calling the Core class$wq=NewWQ ();
(5) Create the system core class under the core directory Wq WQ.class.php
<?PHP//system Core Classclasswq{//variable member (an array that can drop countless variables) Private $BianLiang=Array(); //Construction Method Public function__construct () {if(!Is_dir(Moban) | | !Is_dir(Bianyi)) { Exit("Template directory: Moban or compiled directory: Bianyi does not exist!") Please manually create the "); } } //methods for registering variables Public functionAssign$key,$value) { if(Empty($key)) { Exit("ERROR: Registered variable name cannot be empty"); } $this->bianliang["$key"]=$value; } //methods for invoking templates Public functionDisplay$MoBanMing) { //Get template path $MoBanLuJing=moban.$MoBanMing; //fault tolerance when the template does not exist if(!file_exists($MoBanLuJing)) { Exit(' ERROR: template file does not exist '); } //Compiling file Paths $BianYiLuJing=bianyi.MD5($MoBanMing).$MoBanMing.‘. Php; //generate a compiled file if(!file_exists($BianYiLuJing) ||Filemtime($BianYiLuJing) <Filemtime($MoBanLuJing)) { //introducing parsing class files, instantiating parsing classes and invoking parsing methods includeWQ. ' JieXi.class.php '; $JieXi=NewJiexi ($MoBanLuJing); $JieXi->jiexi ($BianYiLuJing); } //loading the compiled file include $BianYiLuJing; } }
(5) Create a template parsing class under core directory Wq JieXi.class.php
<?PHP//Template Parsing Classesclassjiexi{Private $MoBanNeiRong; //Construction Method Public function__construct ($MoBanLuJing) { if(!$this->mobanneirong=file_get_contents($MoBanLuJing)) { Exit(' ERROR: template file read error '); } } //External public Methods Public functionJiexi ($BianYiLuJing) { //Call Common variable parsing method $this-Jiexi_bianliang (); //invoking the Foreach Tag parsing method $this-Jiexi_foreach (); //generate a compiled file if(!file_put_contents($BianYiLuJing,$this-Mobanneirong)) { Exit(' ERROR: Compilation file generation failed '); } } //Parsing Common variables Private functionJiexi_bianliang () {//analytic regularization of assignment variables $ZhengZe= '/<\{\$ ([\w]+) \}>/'; if(Preg_match($ZhengZe,$this-Mobanneirong)) { $TiHuan= "<?php echo \ $this->bianliang[' $ ']?>"; //Replace a parse template variable $this->mobanneirong=Preg_replace($ZhengZe,$TiHuan,$this-Mobanneirong); } } //parsing a foreach label Private functionJiexi_foreach () {//Assignment parsing regular $ZhengZe= "/<\{foreach\s+ ([\w]+) \ ([[\w]+], ([\w]+] \) \}>/"; $ZhengZe _end= "/<\{\/foreach\}>/"; $ZhengZe _nei= "/<\{([\w]+) \}>/"; if(Preg_match($ZhengZe,$this-Mobanneirong)) { if(Preg_match($ZhengZe _end,$this-Mobanneirong)) { //Replace template label foreach $this->mobanneirong=Preg_replace($ZhengZe _end, ' <?php}?> ',$this-Mobanneirong); $TiHuan= "<?php foreach (\ $this->bianliang['"] as \$$2 = \$$3) {?> "; $this->mobanneirong=Preg_replace($ZhengZe,$TiHuan,$this-Mobanneirong); if(Preg_match($ZhengZe _nei,$this-Mobanneirong)) { //replace the foreach tag index and variable $TiHuan= "<?php echo \$$1?>"; $this->mobanneirong=Preg_replace($ZhengZe _nei,$TiHuan,$this-Mobanneirong); } } Else { Exit("Error: foreach tag is not closed"); } } } }
129th class PHP self-made Simple development template (1)