Manual template engine

Source: Internet
Author: User
Use a self-developed template engine: include (SITE_PATH.bGyT.php); $ tnewGyT (); $ t-setGyiCharset (UTF-8); $ t-setFrontSeparator ({@#); $ t-setBackSeparator (#@}); set the displayed file name $ t-setInVtrParameter (a1, guoyong I am 1); $ t-setInVtrParameter (a2, g

How to Use the manual template engine: include (SITE_PATH. /B/GyT. php); $ t = new GyT (); $ t-setGyiCharset (UTF-8); $ t-setFrontSeparator ({#); $ t-setBackSeparator (#@}); // set the displayed file name $ t-setInVtrParameter (a1, guoyong I am 1); $ t-setInVtrParameter (a2, g

Manual template engine
Usage:
Include (SITE_PATH. "/B/GyT. php ");
$ T = new GyT ();
$ T-> setGyiCharset ("UTF-8 ");
$ T-> setFrontSeparator ("{@#");
$ T-> setBackSeparator ("#@}");
// Set the displayed file name
$ T-> setInVtrParameter ("a1", "guoyong I am 1 ");
$ T-> setInVtrParameter ("a2", "guoyong I am 2 ");
$ T-> setInVtrParameter ("a3", "guoyong I am 3 ");
$ T-> setInVtrParameter ("a4", "guoyong I am 4 ");
$ T-> setInVtrParameter ("a5", "guoyong I am 5 ");

$ T-> setInIfParameter ("guoyong", 1 );
$ A = array (
Array ("Id" => "1", "Title" => "Test 1 "),
Array ("Id" => "2", "Title" => "Test 2 "),
Array ("Id" => "3", "Title" => "Test 3 "),
Array ("Id" => "4", "Title" => "Test 4 "),
Array ("Id" => "5", "Title" => "Test 5 ")
);

$ B = array (
Array ("Id" => "1", "Title" => "Test 1 "),
Array ("Id" => "2", "Title" => "Test 2 "),
Array ("Id" => "3", "Title" => "Test 3 "),
Array ("Id" => "4", "Title" => "Test 4 "),
Array ("Id" => "5", "Title" => "Test 5 ")
);
$ T-> setInForParameter ("netlist", $ );
$ T-> setInForParameter ("netlist1", $ B );
$ FileName = SITE_PATH. "/s/t/default/index1.html ";
$ T-> setFile ($ FileName );
$ T-> parse ();
Echo $ t-> Out ();

 _ GyContent) {return ($ this-> _ gyContent);} else {return (NULL) ;}} private function setContent ($ content) {$ this-> _ gyContent = $ content;} private function getGyiCharset () {if (isset ($ this-> _ gyiCharset )) {return ($ this-> _ gyiCharset);} else {return (NULL) ;}} public function setGyiCharset ($ gyiCharset) {$ this-> _ gyiCharset = $ gyiCharset ;} public function setFrontSeparator ($ gytemp) {$ this-> _ gyFrontSeparator = $ gytemp;} private function getFrontSeparator () {return $ this-> _ gyFrontSeparator ;} public function setBackSeparator ($ gytemp) {$ this-> _ gyBackSeparator = $ gytemp;} private function getBackSeparator () {return $ this-> _ gyBackSeparator ;} public function setFile ($ gyfile) {$ this-> _ gyFile = $ gyfile;} private function getFile () {return $ this-> _ gyFile ;} public function setInVtrParameter ($ ParameterName, $ ParameterVale) {// determines whether the key exists if (array_key_exists ($ ParameterName, $ this-> _ gyInVar )) {// $ this-> _ gyInVar [$ ParameterName] = $ ParameterVale;} else {// $ tempA = array ($ ParameterName => $ ParameterVale) already exists ); $ tempB = $ this-> _ gyInVar; $ this-> _ gyInVar = array_merge ($ tempA, $ tempB) ;}} public function setInIfParameter ($ ParameterName, $ ParameterVale) {// determine whether the key exists if (array_key_exists ($ ParameterName, $ this-> _ gyInIf )) {// $ this-> _ gyInIf [$ ParameterName] = $ ParameterVale;} else {// $ tempA = array ($ ParameterName => $ ParameterVale) already exists ); $ tempB = $ this-> _ gyInIf; $ this-> _ gyInIf = array_merge ($ tempA, $ tempB) ;}} public function setInForParameter ($ ParameterName, $ ParameterVale) {if (array_key_exists ($ ParameterName, $ this-> _ gyInFor) {// $ this-> _ gyInFor [$ ParameterName] = $ ParameterVale ;} else {// $ tempA = array ($ ParameterName => $ ParameterVale); $ tempB = $ this-> _ gyInFor; $ this-> _ gyInFor = array_merge ($ tempA, $ tempB) ;}} public function GyT () {} private function GYReadFile ($ filename) {$ content = file_get_contents ($ filename ); if (is_null ($ this-> getGyiCharset () = TRUE) {$ content = iconv ($ this-> getGyiCharset (), "UTF-8 // IGNORE ", $ content);} return $ content;} public function parse () {try {$ this-> setContent ($ this-> GYReadFile ($ this-> _ gyFile )); // processing tag $ GySplitA = explode ($ this-> _ gyFrontSeparator, $ this-> _ gyContent); if (count ($ GySplitA) <= 1) {// No variable to be processed} else {for ($ I = 1, $ temp_count = count ($ GySplitA); $ I <$ temp_count; $ I ++) {$ GySplitB = explode ($ this-> _ gyBackSeparator, $ GySplitA [$ I]); $ this-> _ gyParameterArray [($ i-1)] = $ GySplitB [0];} $ this-> RepInclude (); // process and Judge $ this-> RepIf (); // process loops $ this-> RepFor (); // processing plug-in // $ this-> RepPlus (); // processing the replaced tag $ this-> RepString () ;}} catch (Exception $ e) {print $ e-> getMessage (); exit ;}} private function RepInclude () {$ tempS = $ this-> _ gyContent; $ tempCountA = count ($ this-> _ gyParameterArray); $ deletekey = array (); for ($ I = 0; $ I <= $ tempCountA; $ I ++) {if (substr_count ($ this-> _ gyParameterArray [$ I], "include:")> 0) {// obtain the content of the contained File $ tempPath = str_replace ("include: "," ", $ this-> _ gyParameterArray [$ I]); $ tempContent = $ this-> GYReadFile (SYSTEM_PATH. $ tempPath); $ tempS = str_replace ($ this-> _ gyFrontSeparator. $ this-> _ gyParameterArray [$ I]. $ this-> _ gyBackSeparator, $ tempContent, $ tempS); $ deletekey [] = $ this-> _ gyParameterArray [$ I];} $ this-> setContent ($ tempS);} private function RepString () {$ tempS = $ this-> _ gyContent; $ tempParameterArray = $ this-> _ gyInVar; $ tempCountA = count ($ this-> _ gyParameterArray); $ deletekey = array (); for ($ I = 0; $ I <= $ tempCountA; $ I ++) {// for variable loops, check if there are any conforming variables and replace if (substr_count ($ this-> _ gyParameterArray [$ I], ":") = 0) {foreach ($ tempParameterArray as $ key => $ value) {if ($ key = $ this-> _ gyParameterArray [$ I]) {$ tempS = str_replace ($ this-> _ gyFrontSeparator. $ key. $ this-> _ gyBackSeparator, $ value, $ tempS); $ deletekey [] = $ key; $ tempParameterArray = $ this-> gydeletearraykey ($ tempParameterArray, $ key) ;}}$ this-> setContent ($ tempS);} private function RepIf () {$ tempS = $ this-> _ gyContent; $ GySplitA = explode ($ this-> _ gyFrontSeparator. "endif:", $ tempS); if (count ($ GySplitA) <= 1) {// No variable to be processed} else {for ($ I = 1, $ temp_count = count ($ GySplitA); $ I <$ temp_count; $ I ++) {$ GySplitF = explode ($ this-> _ gyFrontSeparator. "endif:", $ tempS); $ mainkey = ""; $ GySplitB = explode ($ this-> _ gyBackSeparator, $ GySplitF [1]); $ mainkey = $ GySplitB [0]; $ tempGySplit = $ GySplitF [0]; $ GySplitD = explode ($ this-> _ gyFrontSeparator. "if:", $ GySplitF [0]); $ GySplitE = explode ($ this-> _ gyBackSeparator, $ GySplitD [(count ($ GySplitD)-1)]); $ key = $ GySplitE [0]; $ tempSpStart = ""; $ tempSpEnd = ""; $ tempSpIf = ""; $ tempMain_2 = explode ($ this-> _ gyFrontSeparator. "endif :". $ mainkey. $ this-> _ gyBackSeparator, $ tempS); for ($ k = 1; $ k
 
  
_ GyFrontSeparator. "endif :". $ mainkey. $ this-> _ gyBackSeparator;} $ tempSpEnd = substr ($ tempSpEnd, 0, strlen ($ tempSpEnd)-strlen ($ this-> _ gyFrontSeparator. "endif :". $ mainkey. $ this-> _ gyBackSeparator); // obtain the first $ tempMain_3 = explode ($ this-> _ gyFrontSeparator. "if :". $ key. $ this-> _ gyBackSeparator, $ tempMain_2 [0]); $ tempSpIf = $ tempMain_3 [(count ($ tempMain_3)-1)]; for ($ k = 0; $ k <(count ($ tempMain_3)-1); $ k ++) {$ tempSpStart = $ tempSpStart. $ tempMain_3 [$ k]. $ this-> _ gyFrontSeparator. "if :". $ key. $ this-> _ gyBackSeparator;} $ tempSpStart = substr ($ tempSpStart, 0, strlen ($ tempSpStart)-strlen ($ this-> _ gyFrontSeparator. "if :". $ key. $ this-> _ gyBackSeparator); foreach ($ this-> _ gyInIf as $ ifkey => $ ifvalue) {if ($ mainkey = $ ifkey) {// call the if class for processing $ igyif = new GyIf (); $ igyif-> setTempValue ($ ifvalue); $ igyif-> setgyParameter ($ key ); $ igyif-> settempSpStart ($ tempSpStart); $ igyif-> settempSpEnd ($ tempSpEnd); $ igyif-> settempSpIf ($ tempSpIf ); $ igyif-> setgyFrontSeparator ($ this-> _ gyFrontSeparator); $ igyif-> setgyBackSeparator ($ this-> _ gyBackSeparator); $ igyif-> Parse (); $ tempS = $ igyif-> getContent () ;}}$ this-> setContent ($ tempS);} private function RepFor () {$ tempS = $ this-> _ gyContent; $ GySplitA = explode ($ this-> _ gyFrontSeparator. "endfor:", $ tempS); if (count ($ GySplitA) <= 1) {// No variable to be processed} else {for ($ I = 1, $ temp_count = count ($ GySplitA); $ I <$ temp_count; $ I ++) {$ GySplitF = explode ($ this-> _ gyFrontSeparator. "endfor:", $ tempS); $ mainkey = ""; $ GySplitB = explode ($ this-> _ gyBackSeparator, $ GySplitF [1]); $ mainkey = $ GySplitB [0]; $ tempGySplit = $ GySplitF [0]; $ GySplitD = explode ($ this-> _ gyFrontSeparator. "for:", $ GySplitF [0]); $ GySplitE = explode ($ this-> _ gyBackSeparator, $ GySplitD [(count ($ GySplitD)-1)]); $ key = $ GySplitE [0]; $ tempSpStart = ""; $ tempSpEnd = ""; $ tempSpFor = ""; $ tempMain_2 = explode ($ this-> _ gyFrontSeparator. "endfor :". $ mainkey. $ this-> _ gyBackSeparator, $ tempS); for ($ k = 1; $ k
  
   
_ GyFrontSeparator. "endfor :". $ mainkey. $ this-> _ gyBackSeparator;} $ tempSpEnd = substr ($ tempSpEnd, 0, strlen ($ tempSpEnd)-strlen ($ this-> _ gyFrontSeparator. "endfor :". $ mainkey. $ this-> _ gyBackSeparator); // obtain the first $ tempMain_3 = explode ($ this-> _ gyFrontSeparator. ":". $ key. $ this-> _ gyBackSeparator, $ tempMain_2 [0]); $ tempSpFor = $ tempMain_3 [(count ($ tempMain_3)-1)]; for ($ k = 0; $ k <(count ($ tempMain_3)-1); $ k ++) {$ tempSpStart = $ tempS PStart. $ tempMain_3 [$ k]. $ this-> _ gyFrontSeparator. ":". $ key. $ this-> _ gyBackSeparator;} $ tempSpStart = substr ($ tempSpStart, 0, strlen ($ tempSpStart)-strlen ($ this-> _ gyFrontSeparator. ":". $ key. $ this-> _ gyBackSeparator); foreach ($ this-> _ gyInFor as $ forkey => $ forvalue) {if ($ mainkey = $ forkey) {$ igyfor = new GyFor (); $ igyfor-> setTempValue ($ forvalue); $ igyfor-> setgyParameter ($ key); $ igyfor-> settempSpStart ($ tempSpSt Art); $ igyfor-> settempSpEnd ($ tempSpEnd); $ igyfor-> settempSpFor ($ tempSpFor); $ igyfor-> setgyFrontSeparator ($ this-> _ gyFrontSeparator ); $ igyfor-> setgyBackSeparator ($ this-> _ gyBackSeparator); $ igyfor-> Parse (); $ tempS = $ igyfor-> getContent ();}}}} $ this-> setContent ($ tempS);} private function gydeletearraykey ($ sArray, $ key) {$ tempA = $ sArray; if (array_key_exists ($ key, $ tempA )) {unset ($ tempA [$ key]);} return $ tempA;} private Function gydeletearrayvalue ($ sArray, $ value) {$ tempA = $ sArray; unset ($ tempA [array_search ($ value, $ tempA)]); return $ tempA ;} private function gyshuffle ($ sArray) {$ tempA = $ sArray; $ tempB = array (); $ I = 0; foreach ($ tempA as $ key => $ value) {$ tempB [$ I] = $ value; $ I ++;} return $ tempB;} public function Out () {return $ this-> _ gyContent ;}}?>
  
 

 _ Content = "";} public function setgyFrontSeparator ($ gyFrontSeparator) {$ this-> _ gyFrontSeparator = $ gyFrontSeparator;} public function setgyBackSeparator ($ gyBackSeparator) {$ this-> _ gyBackSeparator = $ gyBackSeparator;} public function setTempValue ($ tempValue) {$ this-> _ tempValue = $ tempValue;} public function setgyParameter ($ gyParameter) {$ this-> _ gyParameter = $ gyParameter;} public function settempSpStart ($ tempSpStart) {$ this-> _ tempSpStart = $ tempSpStart;} public function settempSpEnd ($ tempSpEnd) {$ this-> _ tempSpEnd = $ tempSpEnd;} public function settempSpFor ($ tempSpFor) {$ this-> _ tempSpFor = $ tempSpFor;} public function Parse () {if (is_array ($ this-> _ tempValue) = true) {// judge the array dimension $ weidu = $ this-> arrayLevel ($ this-> _ tempValue ); if ($ weidu = 1) {// if it is a 1D array, replace it with okforeach ($ this-> _ tempValue as $ forkey => $ forvalue) {$ tkey = $ this-> _ gyFrontSeparator. $ this-> _ gyParameter. ". ". $ forkey. $ this-> _ gyBackSeparator; $ this-> _ tempSpFor = str_replace ($ tkey, $ forvalue, $ this-> _ tempSpFor );} $ this-> _ content = $ this-> _ tempSpStart. $ this-> _ tempSpFor. $ this-> _ tempSpEnd;} elseif ($ weidu = 2) {$ tempS = ""; $ tempT = ""; // if it is a 2-dimensional array, replace for ($ I = 0, $ tc = count ($ this-> _ tempValue, 0) cyclically; $ I <$ tc; $ I ++) {$ tempT = $ this-> _ tempSpFor; foreach ($ this-> _ tempValue [$ I] as $ forkey => $ forvalue) {$ tkey = $ this-> _ gyFrontSeparator. $ this-> _ gyParameter. ". ". $ forkey. $ this-> _ gyBackSeparator; $ tempT = str_replace ($ tkey, $ forvalue, $ tempT);} $ tempS = $ tempS. $ tempT;} $ this-> _ content = $ this-> _ tempSpStart. $ tempS. $ this-> _ tempSpEnd;} else {$ this-> _ content = $ this-> _ tempSpStart. $ this-> _ tempSpEnd;} else {$ this-> _ content = $ this-> _ tempSpStart. $ this-> _ tempSpEnd ;}} /*** return the array dimension * @ param [type] $ arr [description] * @ return [type] [description] */function arrayLevel ($ arr) {$ al = array (0); $ this-> aL ($ arr, $ al); return max ($ al);} function aL ($ arr, & $ al, $ level = 0) {if (is_array ($ arr) {$ level ++; $ al [] = $ level; foreach ($ arr as $ v) {$ this-> aL ($ v, $ al, $ level) ;}} public function getContent () {return $ this-> _ content ;}}

 _ Content = "";} public function setgyFrontSeparator ($ gyFrontSeparator) {$ this-> _ gyFrontSeparator = $ gyFrontSeparator;} public function setgyBackSeparator ($ gyBackSeparator) {$ this-> _ gyBackSeparator = $ gyBackSeparator;} public function setTempValue ($ tempValue) {$ this-> _ tempValue = $ tempValue;} public function setgyParameter ($ gyParameter) {$ this-> _ gyParameter = $ gyParameter;} public function settempSpStart ($ tempSpStart) {$ this-> _ tempSpStart = $ tempSpStart;} public function settempSpEnd ($ tempSpEnd) {$ this-> _ tempSpEnd = $ tempSpEnd;} public function settempSpIf ($ tempSpIf) {$ this-> _ tempSpIf = $ tempSpIf;} public function Parse () {$ c_a = $ this-> _ tempSpStart. $ this-> _ tempSpIf. $ this-> _ tempSpEnd; $ c_ B = $ this-> _ tempSpStart. $ this-> _ tempSpEnd; // analyzes the expression to determine whether it contains spaces. if (substr_count ($ this-> _ gyParameter, "") = 0) {// if no expression exists, determine whether the current value is true or false if (isset ($ this-> _ tempValue) {if ($ this-> _ tempValue = true) {$ this-> _ content = $ c_a;} else {$ this-> _ content = $ c_ B ;}} else {$ mainkey = explode ("", $ this-> _ gyParameter); if (count ($ mainkey) = 2) {if ($ this-> _ tempValue = $ mainkey [1]) {$ this-> _ content = $ c_a ;} else {$ this-> _ content = $ c_ B;} else {$ key1 = $ this-> _ tempValue; $ key2 = $ mainkey [1]; // expression $ key3 = $ mainkey [2]; // echo"
------------------
"; // Print_r ($ key2); // echo"
------------------
"; Switch ($ key2) {case" eq ": if ($ key1 ==$ key3) {$ this-> _ content = $ c_a ;} else {$ this-> _ content = $ c_ B;} break; case "=": if ($ key1 = $ key3) {// echo"
-------- True ----------
"; $ This-> _ content = $ c_a;} else {// echo"
-------- False ----------
"; $ This-> _ content = $ c_ B;} break; case"> ": if ($ key1> $ key3) {$ this-> _ content = $ c_a ;} else {$ this-> _ content = $ c_ B;} break; case ">=": if ($ key1 >=$ key3) {$ this-> _ content = $ c_a;} else {$ this-> _ content = $ c_ B;} break; case "<": if ($ key1 <$ key3) {$ this-> _ content = $ c_a;} else {$ this-> _ content = $ c_ B;} break; case "<= ": if ($ key1 <= $ key3) {$ this-> _ content = $ c_a;} else {$ this-> _ content = $ c_ B;} break; default :}}} public function getContent () {return $ this-> _ content ;}}

 {# A5 #@}{@ # Include:/s/t/default/head.html #@}
Test variable 1 {# a1 #@}
{@ # Include:/s/t/default/head.html #@}
{@ # A2 #@}
{@ # A3 #@}
{@ # A4 #@}

{## If: guoyong> 1 #@}
1: {# a4 ##}{# if: guoyong = 1 #@}
1: {@# a4 #@}
{# Endif: guoyong #@}
{# Endif: guoyong #@}
{@ # A4 #@}
{# If: guoyong 1 #@}
2: {@ # a4 #@}
{# Endif: guoyong #@}

{# If: guoyong 1 #@}
3: {@ # a4 #@}
{# Endif: guoyong #@}
{@ # Include:/s/t/default/foot.html ##}{#: netlist #@}===============================
{# Netlist. Title #@}
==================================
{## For: netlist1 #@}
{# Netlist1.Title #@}
{# Endfor: netlist1 ##}{# endfor: netlist #@}
**************************



{@# Plus: test = fdsafdsafsdafsadfdsfdsafdas #@}




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.