本文介绍了如何在模板引擎中解析语句,循环...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我不明白模板引擎解析循环,语句和那种东西的方式背后的逻辑。我的意思是模板引擎,然后是PHP模板引擎。 这样做主要是为了更好地学习PHP,我一直想看看能不能。 这是我尝试过的: 假设我的模板文件中有以下内容: < if> $ variable!= true< / if> //我的代码< elseif> $ variable =='apples'< / elseif> //我的代码< / endif> 要解析它,我尝试了以下变种: <?php $ contents = preg_replace ( / \< if \>(。*)\< \ / if \ > /, <?php if($ 1){,$内容); $ contents = preg_replace( / \\ \\< elseif \>(。*)\< \ / elseif \> /, } elseif($ 1){,$ contents); $ contents = str_replace( < / endif>, }?>,$ contents); $ contents = eval($ contents); ?> 如果我在评估之前回复,我会得到以下信息: <?php if($ variable!= true){ //我的代码} elseif($ variable =='apples '){ //我的代码}?> 在我评估之后,我收到以下错误: 解析错误:语法错误,意外'<'在C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b我正在尝试评估的整个页面,如下所示: < html> < head> < title>测试视图模板引擎< / title> < / head> < body> < p>这是测试,对吧?我的标题< / p> < p>测试测试< / p> <?php if($ variable!= true){ //我的代码} elseif($ variable =='apples'){ //我的代码}?> < / body> < / html> 我也试过以下变种: $ match = array(); preg_match_all( / \< if \>(。*)\< \\ \\ / if\> /,$ contents,$ match [1]); preg_match_all( / \< \ / if \>(。*)\ < elseif \> /,$ contents,$ match [2]); preg_match_all( / \< elseif \>(。*)\< \\ \\ / elseif \> /,$ contents,$ match [3]); preg_match_all( / \< \ / elseif \>(。*)\ < \ / endif\> /,$ contents,$ match [4]); print_r($ match); 这给了我以下数组: 数组( [1] =>数组( [0] =>数组( [0] =>< if> $ variable!= true< / if> ) [1] =>数组( [0] => $ variable!= true ) ) [2] =>数组( [0] =>数组() [1] =>数组() ) [3] = >数组( [0] =>数组( [0] =>< elseif> $ variable =='apples'< / elseif> ) [1] =>数组( [0] => $ variable =='apples') ) [4] =>数组( [0] =>数组() [1] => Arra y () ) ) 出于某种原因,preg_match_all是找不到< / endif>之间的东西和< / elseif>和< elseif>之间和< / if> ... 感谢任何富有成效的评论/帮助。美好的一天!解决方案 variable!= true< / if> //我的代码< elseif> variable =='apples'< / elseif> //我的代码< / endif> 要解析它,我尝试了以下变种: <?php contents = preg_replace( / \< if \>(。*) \< \ / if \> /, <?php if( I don't understand the logic behind the way template engines parse loop, statements and that sort of stuff. I mean template engines other then a PHP template engine.Doing this to learn PHP better mainly, and I've always wanted to see if I could.Here's what I've tried:Let's say I have the following in my template file:<if>$variable != true</if>// My Code<elseif>$variable == 'apples'</elseif>// My Code</endif>To parse that, I've tried the following variants:<?php$contents = preg_replace("/\<if\>(.*)\<\/if\>/", "<?php if($1) {", $contents);$contents = preg_replace("/\<elseif\>(.*)\<\/elseif\>/", "} elseif($1) {", $contents);$contents = str_replace("</endif>", "} ?>", $contents); $contents = eval($contents);?>Which if I echo before I eval it, I get the following:<?php if($variable != true) {// My Code} elseif($variable == 'apples') {// My Code} ?>And after I eval, I get the following error:Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\www\hmvc\includes\template_engine.php(84) : eval()'d code on line 1The entire page I'm trying to eval, looks like:<html> <head> <title>Test View Template Engine</title> </head> <body> <p>This is test, huh? My Title</p> <p>Testing the test</p> <?php if($variable != true) { // My Code } elseif($variable == 'apples') { // My Code } ?> </body></html>I've also tried the following variant:$match = array();preg_match_all("/\<if\>(.*)\<\/if\>/", $contents, $match[1]);preg_match_all("/\<\/if\>(.*)\<elseif\>/", $contents, $match[2]);preg_match_all("/\<elseif\>(.*)\<\/elseif\>/", $contents, $match[3]);preg_match_all("/\<\/elseif\>(.*)\<\/endif\>/", $contents, $match[4]);print_r($match);Which gives me the following array:Array( [1] => Array ( [0] => Array ( [0] => <if>$variable != true</if> ) [1] => Array ( [0] => $variable != true ) ) [2] => Array ( [0] => Array ( ) [1] => Array ( ) ) [3] => Array ( [0] => Array ( [0] => <elseif>$variable == 'apples'</elseif> ) [1] => Array ( [0] => $variable == 'apples' ) ) [4] => Array ( [0] => Array ( ) [1] => Array ( ) ))For some reason the preg_match_all is not finding the stuff between </endif> and </elseif> and between <elseif> and </if>...Thanks for any productive comments/help. Good day! 解决方案 variable != true</if>// My Code<elseif>variable == 'apples'</elseif>// My Code</endif>To parse that, I've tried the following variants:<?phpcontents = preg_replace("/\<if\>(.*)\<\/if\>/", "<?php if( 这篇关于如何在模板引擎中解析语句,循环...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 06:04