punk_runner Posted January 15, 2011 Share Posted January 15, 2011 I am working on a very simple templating engine and I ran into a problem. Here's the basic template.class.php file <?php class Template { public $template; function load($filepath) { $this->template = file_get_contents($filepath); } function replace($var, $content) { $this->template = str_replace("#$var#", $content, $this->template); } function publish() { eval("?>".$this->template."<?"); } } ?> That is called in whatever model I am using, like this: public function createTemplate() { $templatePath = TEMPLATE_PATH . strtolower(__CLASS__) . ".php"; require_once (APPLICATION_PATH . 'libraries/template.class.php'); $template = new Template; $template->load($templatePath); $template->replace("productid", $this->product_id); $template->replace("price", $this->price); $template->replace("sidebar_message", $this->sidebarmsg); $template->publish(); } My problem is that my template.php ($templatePath) file uses include() to bring in the sidebar, head and footer. Any values that I passed to the template which are in the sidebar, header or footer are not evaluated. They just show their placeholders like #sidebar_message#... Is there another way to do this and publish the template so that they are evaluated properly? Link to comment https://forums.phpfreaks.com/topic/224548-using-include-within-a-file-parsed-with-eval/ Share on other sites More sharing options...
punk_runner Posted January 15, 2011 Author Share Posted January 15, 2011 Just realized that the replace() method is run before the eval() so the included files are not part of that... *heads back to workbench* Link to comment https://forums.phpfreaks.com/topic/224548-using-include-within-a-file-parsed-with-eval/#findComment-1159912 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.