Computer Science Canada

Including PHP script in templating system

Author:  Aziz [ Fri Aug 01, 2008 12:58 pm ]
Post subject:  Including PHP script in templating system

Hey guys. While working on the company website, I've implemented a small menu system. The templates, for example, "about.page.php" are as follows:

code:
{% Title: About}

<p>Some text would go in a paragraph. Along with other HTML elements like:</p>
<ul>
<li>Lists</li>
<li>Images</li>
<li>etc</li>
</ul>


The appropriate .page.php file is used to construct a Page class (based on the request url):

php:
class Page
{
        public $title, $body;
        public $sidebar = false;
       
        public function __construct($filename, $replace = NULL)
        {
                $f = fopen($filename, 'rb');
                $body = fread($f, filesize($filename));
                fclose($f);
               
                $title = '';
                $sidebar = false;
               
                preg_match('/{% title: (.+)}/im', $body, $matches);
                if ($matches[1]) $title = $matches[1];
               
                preg_match('/{% sidemenu: (.+)}/im', $body, $matches);
                $sidebar = (strtolower($matches[1]) == 'yes');
               
                // Replace any specified values
                if ($replace)
                {
                        foreach ($replace as $key => $value)
                        {
                                $body = str_ireplace("{% $key}", $value, $body);
                        }
                }
               
                // Strip contents of tags
                $body = preg_replace('/{%.*}.?\n/i', '', $body);
               
                $this->title = $title;
                $this->sidebar = $sidebar;
                $this->body = $body;
        }
}


However, I may want to include some PHP code on certain pages, like a news feed, etc. What would be the best way to go about this?


: