StrangeWill Posted December 2, 2006 Share Posted December 2, 2006 I read using eval() is a lot more efficient way than say... downloading smarty templates (just adding the templates, not even fully implementing them, triples my page load time), but I'm having a major problem:[code=php:0]$RegEx = "/{(.*)\.(.*)}/";$RegExReplace = '$this->Site->Config[\'\\1\'][\'\\2\']';$Data = preg_replace($RegEx, $RegExReplace, $Data, PREG_SET_ORDER);eval("\$Data = \"$Data\";");echo($Data);[/code]$Data:[code]<p>{site.title}</p><p>{site.path}</p>[/code]$Data before eval()[code]<p>$this->Site->Config['site']['title']</p><p>$this->Site->Config['site']['path']</p>[/code]Good :D$Data after eval()[code]<p>Object id #1->Config['site']['title']</p><p>Object id #1->Config['site']['path']</p>[/code]Object id? WTF?! No bad! BAD code! Run the WHOLE code, ugh!If I do a echo($this->Site->Config['site']['path']); it prints the code fine... some kind of issue with eval() Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/ Share on other sites More sharing options...
StrangeWill Posted December 3, 2006 Author Share Posted December 3, 2006 I have also tried passing $Config[][] vars to a local variable, no luck.I'd rather do straight up variable passing though. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134155 Share on other sites More sharing options...
StrangeWill Posted December 3, 2006 Author Share Posted December 3, 2006 Hmm take it eval() isn't used often. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134167 Share on other sites More sharing options...
Philip Posted December 3, 2006 Share Posted December 3, 2006 [quote=PHP Manual]To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134179 Share on other sites More sharing options...
StrangeWill Posted December 3, 2006 Author Share Posted December 3, 2006 New code:[code]?><p>$this->Site->Config['site']['title']?></p><p>$this->Site->Config['site']['path']?></p><?[/code]Comes out:[code]?><p><?=Object id #1->Config['site']['title']?></p><p><?=Object id #1->Config['site']['path']?></p><?[/code]It doesn't process it even though that runs through the eval() Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134412 Share on other sites More sharing options...
StrangeWill Posted December 3, 2006 Author Share Posted December 3, 2006 Now if I could somehow say "include the results of this into a variable" that would be awesome!Since I can't I'm using eval() Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134423 Share on other sites More sharing options...
StrangeWill Posted December 4, 2006 Author Share Posted December 4, 2006 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134651 Share on other sites More sharing options...
keeB Posted December 4, 2006 Share Posted December 4, 2006 You're not making any sense. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134683 Share on other sites More sharing options...
StrangeWill Posted December 4, 2006 Author Share Posted December 4, 2006 I want to process this:[code]<p>$this->Site->Config['site']['title']</p><p>$this->Site->Config['site']['path']</p>[/code]Which is a string, as if it was code. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134703 Share on other sites More sharing options...
trq Posted December 4, 2006 Share Posted December 4, 2006 Can we see the code you've tried? You are also aware that the keyword $this will not work outside of an objects context? This is more than likely your issue buit I would need to see code first. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134706 Share on other sites More sharing options...
StrangeWill Posted December 4, 2006 Author Share Posted December 4, 2006 [quote author=thorpe link=topic=117155.msg478295#msg478295 date=1165217476]Can we see the code you've tried? You are also aware that the keyword $this will not work outside of an objects context? This is more than likely your issue buit I would need to see code first.[/quote]Yes, I'm using objects, using: echo($this->Site->Config['site']['title']) will print the correct data.Basically I'm taking this:[code]<p>{site.title}</p><p>{site.path}</p>[/code]I'm translating it into PHP via RegEx:[code]<p>$this->Site->Config['site']['title']</p><p>$this->Site->Config['site']['path']</p>[/code](or if this is better I can do this)[code]<p><?=$this->Site->Config['site']['title']?></p><p><?=$this->Site->Config['site']['path']?></p>[/code]I COULD cache this to a file, then include() it, but that becomes rather messy, I'd much rather process it as IF it was a php page into the $Data string, I read you do this with eval(), and then insert it into the page. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134839 Share on other sites More sharing options...
trq Posted December 4, 2006 Share Posted December 4, 2006 We need to see actual code!! Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134849 Share on other sites More sharing options...
StrangeWill Posted December 4, 2006 Author Share Posted December 4, 2006 Sorry didn't have access till this morning:[code=php:0]class Wolfram_Studios_Template extends Wolfram_Studios_Base{ //Template data in array var $TmpVar; var $TmpDir; var $TmpMain; function Wolfram_Studios_Template($inSite) { $this->Name = "Wolfram Studios Template"; $this->Version = "1.0.0"; $this->Site = $inSite; $this->Site->AddComponent($this); $this->TmpDir = $this->Site->Config['site']['path']; $this->TmpMain = $this->Site->Config['site']['path'] . $this->Site->Config['site']['template_path'] . 'main.tpl'; } function DisplayTemplate($TemplateName) { //Open the template and get all the variables ({var}) $Filename = $this->TmpDir . $TemplateName; $File = fopen($Filename, 'r'); $FileSize = filesize($Filename); $Data = fread($File, $FileSize); fclose($File); $RegEx = "/{(.*)\.(.*)}/"; $RegExReplace = '$this->Site->Config[\'\\1\'][\'\\2\']'; $Data = preg_replace($RegEx, $RegExReplace, $Data, PREG_SET_ORDER); eval("\$Data = \"$Data\";"); echo($Data); }}class Wolfram_Studios_Base{ //Parent Site var $Site; //This component's data var $Version; var $Name; var $Requirements; function AddRequirement($InComponenetName, $InComponentVersion) { $this->Requirements[sizeof($this->Requirements)] = array('Name' => $InComponenetName, 'Version' => $InComponentVersion); } }[/code]I call DisplayTemplate($TemplateName) under the Wolfram_Studios_Template object I created. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-134978 Share on other sites More sharing options...
StrangeWill Posted December 4, 2006 Author Share Posted December 4, 2006 El Bumpz0r Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-135197 Share on other sites More sharing options...
StrangeWill Posted December 5, 2006 Author Share Posted December 5, 2006 Bump(Hehe now that I posted all the code no one cares :P... I'm patient, it is kind of hard to figure this one out...) Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-135328 Share on other sites More sharing options...
StrangeWill Posted December 5, 2006 Author Share Posted December 5, 2006 Bump Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-135386 Share on other sites More sharing options...
trq Posted December 5, 2006 Share Posted December 5, 2006 Yeah Im sorry, but I really cant see what it is your trying to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-135388 Share on other sites More sharing options...
StrangeWill Posted December 5, 2006 Author Share Posted December 5, 2006 [quote author=thorpe link=topic=117155.msg478989#msg478989 date=1165312641]Yeah Im sorry, but I really cant see what it is your trying to achieve.[/quote]Okay.... I got these values:$this->Site->Config['site']['title'] = "test 1";$this->Site->Config['site']['path'] = "test 2";I got a string:[code]<p><?=$this->Site->Config['site']['title']?></p><p><?=$this->Site->Config['site']['path']?></p>[/code]I want it to output:[code]<p>test 1</p><p>test 2</p>[/code]As if it was rendering a PHP page from the string. Run it like code. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-135742 Share on other sites More sharing options...
trq Posted December 5, 2006 Share Posted December 5, 2006 Can we see what $Data looks like before you try and use eval() on it? What is the ouput of...?[code=php:0]$Data = preg_replace($RegEx, $RegExReplace, $Data, PREG_SET_ORDER);die($Data);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-135744 Share on other sites More sharing options...
StrangeWill Posted December 6, 2006 Author Share Posted December 6, 2006 [quote author=thorpe link=topic=117155.msg479348#msg479348 date=1165358475]Can we see what $Data looks like before you try and use eval() on it? What is the ouput of...?[code=php:0]$Data = preg_replace($RegEx, $RegExReplace, $Data, PREG_SET_ORDER);die($Data);[/code][/quote][code]<p>$this->Site->Config['site']['title']</p><p>$this->Site->Config['site']['path']</p>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-135809 Share on other sites More sharing options...
StrangeWill Posted December 6, 2006 Author Share Posted December 6, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-135948 Share on other sites More sharing options...
StrangeWill Posted December 6, 2006 Author Share Posted December 6, 2006 Maybe I can combine all the strings, save them as a file, then include them?I dunno if thats a good idea or not though... we'll see. Quote Link to comment https://forums.phpfreaks.com/topic/29265-self-made-quick-template-engine-problems-eval-help/#findComment-136246 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.