stoneyrose Posted August 8, 2009 Share Posted August 8, 2009 Hi all, I've been trying in vain to make the following work correctly and can't for the life of me find anything in . I'm making a template engine (can't dump the project and use Smarty) from the ground up and it's working by replacing keywords in a html file like {phpfreaks::isgreat} with either HTML output or PHP using echo or eval respectively. The database table rows are marked accordingly so no problems there until I examine the output, where I find that eval has worked correctly but instead of putting it where the old keyword was it places it at the very beginning, what am I doing wrong and how can I work around it to get the evaluated PHP code where it should be. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/ Share on other sites More sharing options...
Bjom Posted August 8, 2009 Share Posted August 8, 2009 example? Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893666 Share on other sites More sharing options...
stoneyrose Posted August 8, 2009 Author Share Posted August 8, 2009 What do you want the code or the output? Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893669 Share on other sites More sharing options...
wildteen88 Posted August 8, 2009 Share Posted August 8, 2009 Both would help. Post only the relevant parts Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893678 Share on other sites More sharing options...
stoneyrose Posted August 8, 2009 Author Share Posted August 8, 2009 Heres the code, the first loop searches and replaces HTML and the second PHP. //Search and replace template terms that are not PHP $this->res = mysql_query("select * from " . PRE . "confTemplateEngine where isPHP='0'"); while($this->row = mysql_fetch_array($this->res)) { $this->fileContents = str_replace($this->row['searchValue'], $this->row['replaceValue'], $this->fileContents); } //Search for markup used to generate PHP, then eval them and return to fileContents $this->res = mysql_query("select * from " . PRE . "confTemplateEngine where isPHP='1'"); while($this->row = mysql_fetch_array($this->res)) { $this->fileContents = str_replace($this->row['searchValue'], eval($this->row['replaceValue']), $this->fileContents); } The output source is this... It Works!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- body { background-color: #F00; } #apDiv1 { position:absolute; width:200px; height:115px; z-index:1; left: 224px; top: 56px; background-color: #00CCFF; } .sfg { font-weight: bold; font-family: "Lucida Console", Monaco, monospace; font-size: 24px; } #apDiv2 { position:absolute; width:435px; height:115px; z-index:2; left: 582px; top: 183px; } --> </style></head> <body> <div id="apDiv1">qwerty qwerty qwerty success!</div> <p class="sfg"> </p> <p class="sfg"> </p> <p class="sfg">qwerty qwerty qwerty success! </p> <p class="sfg"> </p> <p class="sfg">qwerty qwerty qwerty success!</p> <div id="apDiv2"></div> </body> </html> The "qwerty qwerty qwerty ect" is a successful HTML replace (for example in its place was {qwerty} and it successfully changed it and placed it in the correct location. However the "It Works!" at the top of the page is evaluated code that should be inside "<div id="apDiv2"></div>" where the original "{php::itworks}" was. Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893685 Share on other sites More sharing options...
wildteen88 Posted August 8, 2009 Share Posted August 8, 2009 Can we see the search/replace values that are in your database too Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893688 Share on other sites More sharing options...
stoneyrose Posted August 8, 2009 Author Share Posted August 8, 2009 Yep, these are the two that are being used... searchValue --- replaceValue --- isPHP {test:QWERTY} --- qwerty qwerty qwerty success! --- 0 {test:PHP} --- echo 'It Works!'; ?> --- 1 Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893689 Share on other sites More sharing options...
stoneyrose Posted August 8, 2009 Author Share Posted August 8, 2009 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893736 Share on other sites More sharing options...
Bjom Posted August 8, 2009 Share Posted August 8, 2009 this does not do, what you think it does: $this->fileContents = str_replace($this->row['searchValue'], eval($this->row['replaceValue']), $this->fileContents); this is supposed to simply fill the property "fileContents", right? It doesn't. It immediately executes the php code. That's why it turns up at the beginning of the file. You need to get rid of the eval at this place. I bet you have a different place where you echo "fileContents"? This is where you need to make the distinction between eval and echo. Perhaps make "fileContents" an array and add one entry each time your loop runs. you can loop through it at a later point and decide to either echo or eval the entry. Bjom Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893775 Share on other sites More sharing options...
glenelkins Posted August 8, 2009 Share Posted August 8, 2009 i have an idea...just use Smarty lol I wrote an MVC framework for my own projects ( quite alot in it really! ) with smarty fully integrated for the engine...fantastic Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893778 Share on other sites More sharing options...
stoneyrose Posted August 8, 2009 Author Share Posted August 8, 2009 this does not do, what you think it does: $this->fileContents = str_replace($this->row['searchValue'], eval($this->row['replaceValue']), $this->fileContents); this is supposed to simply fill the property "fileContents", right? It doesn't. It immediately executes the php code. That's why it turns up at the beginning of the file. You need to get rid of the eval at this place. I bet you have a different place where you echo "fileContents"? This is where you need to make the distinction between eval and echo. Perhaps make "fileContents" an array and add one entry each time your loop runs. you can loop through it at a later point and decide to either echo or eval the entry. Bjom Thanks, that's what I wanted to know and you are quite correct about the echo being later on. Quote Link to comment https://forums.phpfreaks.com/topic/169365-solved-creating-a-template-engine-using-echo-and-eval-side-by-side/#findComment-893779 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.