如果有样一个数组 $test=array(1=>'a',2=>'b'); <!-- BEGINB test --> {test} <!-- ENDB test --> <!-- BEGIN test --> {test} <!-- END test --> 这样就不起作用了!我已经也试过在 NEATTemplateCompiler.php 加上 /** * foreach 处理 * @access private * @return void */ function _ParseFOREACH(){ if(preg_match_all('/\[(?:\s*)foreach?:\s*)([a-zA-Z0-9_.]+)(?:\s*)\]/', $this->contents,$reg) ) { $value[$reg[1][0]]='$'.$reg[1][0]; $v[v]='$v'; $phpCode = '<?php' . "\r\n" . 'is_array( ' . $value[$reg[1][0]] . ' ) ? ' . $value[$reg[1][0]]. ': '.$value[$reg[1][0]].'= array();' . "\r\n" . 'foreach( ' . $value[$reg[1][0]] . ' as ' .$v[v]. ')' . "\r\n" . '{' . "\r\n" . '?>'; // 把原始内容替换成对应的PHP代码 /* [foreach:name] 变成 is_array($name)?$name:$name=array(); foreach($name as $value ){ */ $this->contents = str_replace( $reg[0], $phpCode, $this->contents ); // 把原始内容替换成对应的PHP代码 /* [name]变成 echo $value */ $seach='['.$reg[1][0].']'; $phpCode='<?php echo '.$v[v].';?>'; $this->contents = str_replace( $seach, $phpCode, $this->contents); // 把原始内容替换成对应的PHP代码 /* [/foreach]变成 } */ $seach='[/foreach]'; $phpCode='<?php }?>'; $this->contents = str_replace( $seach, $phpCode, $this->contents ); } } 就是模板直接这个用到就可以不设置模板变量 [foreach:test] [test] [/foreach] 生成代码也正常,就是不知道为什么不显示出来? <?php is_array( $test ) ? $test: $test= array(); foreach( $test as $v) { ?> <?php echo $v;?> <?php }?> 最好你们可以马上修改一下这个bugs吧!模板中要到循环本身的数组很多地方都用到的!有这个功能,可以遍历数组的形式就支持得更多!
Code: $tpl['list'][0]['name'] = 'a'; $tpl['list'][1]['name'] = 'b'; $objTP->SV( $tpl ); Template : <!-- BEGIN test --> {test.name} <!-- END test -->