Jason4Ever Posted April 3, 2013 Share Posted April 3, 2013 Hello , i built php class for parsing html templates , and i used preg_replace_callback function for parsing template while loops & if conditions , for loop , ... etc i repeated the function many times , for every process , such as one time for parsing while loop , and another time for if conditions parsing .. etc , and repeated all of these steps again when parsing include files inside the template , but i think that this method of parsing is slow when parsing big template , so i think that using one while loop and read template line by line and compare it then parsing found line will be more fast , so what is your opinion , using one while loop and read the template file line by line and parsing it is more useful ? or using preg_replace_callback ? and is it preferred to parse the codes inside the template ? by replacing it with php code then evaluate the template using eval() ? or parsing it inside the php file then append the result to the template ? Quote Link to comment https://forums.phpfreaks.com/topic/276504-preg_replace_callback-vs-while-loop-line-by-line-for-template-compiler/ Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 What does the template syntax look like? Quote Link to comment https://forums.phpfreaks.com/topic/276504-preg_replace_callback-vs-while-loop-line-by-line-for-template-compiler/#findComment-1422736 Share on other sites More sharing options...
Jason4Ever Posted April 3, 2013 Author Share Posted April 3, 2013 <[iNCLUDE BLOCK : dim_header]> <[sTART BLOCK : welc]> <h2 class="block_title bg0">{$dimphrase.welc_msg|replace values="mooo,ererer"}</h2> <div class="block_content padding bg0">{welc_msg|replace values="ya,yahoo"}</div> <br/> <[END BLOCK : welc]> <[sTART BLOCK : welc_msg2]> <h2 class="block_title bg0">{$dimphrase.welc_msg}</h2> <div class="block_content padding bg0">{welc_msg|str_replace values="1,2,12"}</div> <br/> <[sTART BLOCK : UY]> hii<br /><span>6 <[END BLOCK : UY]> <[sTART BLOCK : GO]> hii<br /><span>5 <[END BLOCK : GO]> <[END BLOCK : welc_msg2]> <[iNCLUDE BLOCK : news_important]> {*TEMPLATE ZONE START:DON'T REMOVE NEXT LINE*} <[iNCLUDEZONE BLOCK : Important_block_module]> {*TEMPLATE ZONE END*} <br /> <[sTART PHP CODE]> echo "ahmoodi"; <[END PHP CODE]> <[sTART PHP CODE]> echo 'yoyoyo'; <[END PHP CODE]> {gogogo|replace values="hi,hello dimofinf"|replace values="hell,hmooda"} {function: randomcolor} <br /> {function:calc name="row" values="1,5,8" sign="+"} <Br /> <br /> <[iNCLUDESCRIPT BLOCK : ./news]> <if " eq '1'"> ahmed </if> this is example , it's very near to template power engine Quote Link to comment https://forums.phpfreaks.com/topic/276504-preg_replace_callback-vs-while-loop-line-by-line-for-template-compiler/#findComment-1422738 Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 (edited) How do you feel about picking one tag format and sticking with it? I see three in there. {}s are traditional so maybe {include block=dim_header} {start block=welc}{$dimphrase.welc_msg|replace values="mooo,ererer"}{welc_msg|replace values="ya,yahoo"} {end block=welc} {start block=welc_msg2}{$dimphrase.welc_msg}{welc_msg|str_replace values="1,2,12"} {start block=UY} hii6 {end block=UY} {start block=GO} hii5 {end block=GO} {end block=welc_msg2} {include block=news_important} {*TEMPLATE ZONE START:DONT REMOVE NEXT LINE*} {includezone block=Important_block_module} {*TEMPLATE ZONE END*} {start code=php} echo "ahmoodi"; {end code=php} {start code=php} echo 'yoyoyo'; {end code=php} {gogogo|replace values="hi,hello dimofinf"|replace values="hell,hmooda"} {function: randomcolor} {function:calc name="row" values="1,5,8" sign="+"} {includescript block=./news} {if " eq '1'" - not sure what youre trying to show here} ahmed {endif} Edited April 3, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/276504-preg_replace_callback-vs-while-loop-line-by-line-for-template-compiler/#findComment-1422752 Share on other sites More sharing options...
Jason4Ever Posted April 3, 2013 Author Share Posted April 3, 2013 yes , my problem is not about tag format , but about ideal and fastest method to parsing templates , is to use preg_replace_callback() many times ? ro use one while loop and read template line by line then parsing it ? Quote Link to comment https://forums.phpfreaks.com/topic/276504-preg_replace_callback-vs-while-loop-line-by-line-for-template-compiler/#findComment-1422754 Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 And I'm saying (implying) that simplifying the template syntax is a step in the right direction. At a minimum right now you'd need three different regexes for the three different formats so that you can simply find tags in the right "format" and then deal with their actual purposes during replacement (a la preg_replace_callback()), but the one that handles the HTML-esque tags will slow down the parsing so much because it'll find HTML tags too. Neither of your solutions are great. That's why I'm trying to steer you in a smarter direction. Quote Link to comment https://forums.phpfreaks.com/topic/276504-preg_replace_callback-vs-while-loop-line-by-line-for-template-compiler/#findComment-1422762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.