This post was last edited by xuzuning on 2012-05-23 15:02:49
Don't say much, just go to the code.
template.php (3.55K)
Find_var ($s); $this->data = Explode (' < ', $s); $this->data[0] = '
'; $this->find_dsn (); }//New Run method, function run () {//include "data://,". Join (' < ', $this->data), eval ('?> '. Join (' < '), $this-> ;d ATA)); The function find ($pattern) {$this->pattern = $pattern; return array_filter ($this->data, Array ($this, ' Find_ Callback ')); The Private Function Find_dsn () {foreach ($this->find ("#\bdsn\b#i") as $k = + $v) {$t = $this->find_tag ($tag = Strtok ( $v, "), $k), End ($t), $dsn [] = Array ($k, key ($t));} if ($this->drillmode) {foreach ($this->find ("#\bdrill\b#i") as $k = + $t) {foreach ($dsn as $i + = $v) if ($k < $v [1] && $k > $v [0]) $t = $i; $drill [] = $dsn [$t];unset ($dsn [$t]);}} foreach ($dsn as $v) {list ($start, $end) = $v;p reg_match ('/\bdsn\s*=\s* ([^\s>]+)/I ', $this->data[$start], $reg); this->data[$start] = Str_replace (". $reg [0]," ', $this->data[$start]); $m = Explode (', ', Trim ($reg [1], ' \ ' ")) + AR Ray (0, 0, "); $code _start ="? php if (isset (\$_var)) \$_st[]=\$_var;foreach ((Isset (\$_var[' $m [0] '])? \$_var[' $m[0] ']:\ $this $m [0] (' $m [1] ', ' $m [2] ') as \$_key=>\$_var) {?> "; $code _end ="? php}\$_var=array_pop (\$_st); > Switch ($m [1]) {case 0: $t = explode (' > ', $this->data[$start]), $t [1] = "< $code _start". $t [1]; $this data[$start] = join (' > ', $t), $this->data[$end] = "$code _end<". $this->data[$end];break;case 1: $this->data[$end]. = "< $code _end"; $this->data[$start] = "$code _start< " . $this->data[$start];break;default: $n = round (100/$m [1]); $this->data[$end]. = "< $code _end "; $this->data[$start] =" $code _start
< ". $this->data[$start];break;}} if ($this->drillmode) foreach ($drill as $v) {list ($start, $end) = $v;p reg_match ('/\bdsn\s*=\s* ([^\s>]+)/I ', $ this->data[$start], $reg), $this->data[$start] = Str_replace (". $reg [0]," ', $this->data[$start]); $m = Explode (', ', Trim ($reg [1], ' \ ' ")) + array (0, 0, '); $code = '; for ($i = $start; $i <= $end; $i + +) {$code. = ' < '. $this ->data[$i];if ($i > $start) unset ($this->data[$i]);} $code = Addslashes ($code); $this->data[$start] = "php \$_code= ' $code '; \ $this->drill (\$_code, Isset (\$_var[' $m [ 0])? \$_var[' $m [0] ']:\ $this $m [0] (' $m [1] ', ' $m [2] ');? > ";} } protected function Find_tag ($tag, $offs =0) {$r = array (); $counter = 0;foreach ($this->find ("#^/? $tag #i") as $k + = $v {if ($k >= $offs) {$counter + = $v {0} = = '/'? 1:-1; $r [$k] = $v; if ($counter = = 0) break;}} return $r; } protected function Find_callback ($v) {return Preg_match ($this->pattern, $v); } Private Function Find_var (& $s) {$s = Preg_replace_callback ('/\{(\w+) \}/', Array ($this, ' var_callback '), $s); } protected function Var_callback ($r) {if ($r [1] = = ' drill ') {$this->drillmode++;return '
Drill ($_code, $_var[\ ' child\ ');? > ';} Return "
"; } protected function Drill ($_code, $_source) {if (empty ($_source) | |! Is_array ($_SOURCE)) return array (); foreach ($_source as $_key=>$_var) {//include ' data://, '. $_code;eval ('?> '. $_code); }} function __call ($func, $param) {if (function_exists ($func)) return Call_user_func_array ($func, $param); return Array ( ); } function __tostring () {return join (' < ', $this->data); }}
Add
There are only two types of control elements in the template:
1. The template variable name is enclosed in curly braces, such as {var}. The function that provides the data should provide the corresponding data, if not provided then automatically empty
2. The data source declares the DSN attribute in the HTML tag, such as
{T}
where Foo represents a function that provides data, and 1 indicates that the loop contains itself when the data is filled, such as the returned data is an array (array (' t ' =>1,array (' t ' =>2))
The actual generated HTML is
1
2
When the second parameter defaults, the same data produces only
12
3. The Data source function convention returns a two-dimensional array: The first dimension is the subscript array, and the second dimension is the associative array. See test examples
Writing a program is simple, and writing a document is killing me.
See #39 for answers to related questions
The usage of an extension is shown in #61
Reply to discussion (solution)
This post was last edited by xuzuning on 2012-05-23 08:40:52
Test example
Include ' template.php '; function body () {return array (' title ' = ' + ' layout ', ' advertising ' = ' = ') always like to use Table layout, this div+css way is very annoying ', ' menu ' = = Array (' text ' = ' = ' home '), array (' text ' = = ' blog '), Array (' text ' = ' + ' design '), Array (' text ' = ' = ' album '), Array (' text ' = ' forum '), Array (' Te XT ' = ' about '), ' footer ' = ' Why does it behave differently, ie is aligned ', ');} function img () {$ar [' url '] = ' http://justinyoung.cnblogs.com/images/cnblogs_com/justinyoung/2007/tb_m.PNG '; return Array ($ar);} function tree () {$data = array (' ID ' =>1, ' parent ' =>0, ' NAME ' = ' grandfather '), array (' ID ' =>2, ' parent ' =>1, ' N AME ' = ' father '), array (' ID ' =>3, ' parent ' =>1, ' name ' = ' uncles '), array (' ID ' =>4, ' parent ' =>2, ' name ' = = ') Own '), array (' ID ' =>5, ' parent ' =>4, ' NAME ' = ' son '), $t = Find_child ($data, ' id ', ' PARENT ');//print_r ($t); return Array ($t [1]);} function box () {return array (' t ' = '--1--', ' chiLD ' = = Array (array (' t ' =>2), array (' t ' =>2), array (' t ' =>2),), Array (' t ' = = '--2--', ' child ' = = Array ( Array (' t ' =>2), array (' t ' =>2), array (' t ' =>2),), Array (' t ' = ' = '--3--', ' child ' = = = Array (' t ' =>2) , Array (' t ' =>2), array (' t ' =>2),), Array (' t ' = '--4--', ' child ' = = Array (' t ' =>2), array (' t ' =>2 ), Array (' t ' =>2),), Array (' t ' = = '--5--', ' child ' = = = Array (' t ' =>2), array (' t ' =>2), array (' t ' = 2)), array (' t ' = = '--6--', ' child ' = = Array (array (' t ' =>2), array (' t ' =>2), array (' t ' =>2),));} $p = new Template (' Layout. txt '); $p->run (); Use also to make corresponding adjustments
Template file (layout. txt)
<title>{title}</title>
{Advertising}
{T} {T} {footer}
ci1699 Amysql catch up with phpMyAdmin;
Moderator this is to overtake Smarty AH.
Don't say anything, get it down and try it first!
Can you add some comments ah, hard to see not quite understand, careful look is better than a fierce look
Just need a template, haha, thank you so much
First make a mark, wait for the time to study slowly.
Can you add some comments ah, hard to see not quite understand, careful look is better than a fierce look
Just need a template, haha, thank you all get down, try to run it. You should understand.
Xuzuning?? The Accidental theft number? There's very little mark at night to see
Xuzuning?? The Accidental theft number? It's very rare. Mark the night to see if you didn't want me to hang my template engine last time? It was wrapped in a toolbox, and this time it was stripped out.
Yes, it's bull B. The strongman.
Watch the textbook ... Take two days to read the product carefully.
Fatal error:call to undefined function find_child () inch E:\test.local\test.php on line 51
This error has occurred. What's going on here?
The Find_child () function is one of the two tree-shaped array constructors that I last sent without a recursive function
Oh, yes, it's supported.
Don't understand:
Warning:include () [function.include]: data://wrapper is disabled in the server configuration by allow_url_include=0 in \ path\template_csdn\template_class.php on line 13warning:include (data://,
Body (' 0 ', ')) as $_key=>$_var) {?>
<title><?php echo isset ($_var[' title ')? $_var[' title ']: ';? ></title>