Jump to content

Using include() within a file parsed with eval()...


punk_runner

Recommended Posts

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.