MVC Help with templating

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

MVC Help with templating

Post by mischievous »

Hey guys, I've recently gone through a tutorial for building a MVC from the ground up... and I have everything up and running!
tutorial can be found at (http://anantgarg.com/2009/03/13/write-y ... rk-part-1/).

This has it setup to have a template setup for the views and or default controller... Im trying to setup a master template that would surround all of that?

The code is as follows for template.class.php:

Code: Select all

<?php
class Template {
    
    protected $variables = array();
    protected $_controller;
    protected $_action;
    
    function __construct($controller,$action) {
        $this->_controller = $controller;
        $this->_action = $action;
    }
 
    /** Set Variables **/
    function set($name,$value) {
        $this->variables[$name] = $value;
    }
 
    /** Display Template **/
    function render() {
        extract($this->variables);
 
            if (file_exists(ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . 'header.php')) {
                include (ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . 'header.php');
            } else {
                include (ROOT . DS . 'application' . DS . 'views' . DS . 'header.php');
            }
 
        include (ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . $this->_action . '.php');         
 
            if (file_exists(ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . 'footer.php')) {
                include (ROOT . DS . 'application' . DS . 'views' . DS . $this->_controller . DS . 'footer.php');
            } else {
                include (ROOT . DS . 'application' . DS . 'views' . DS . 'footer.php');
            }
    }
 
}
 
any help or pointing in the right direction would be greatly appreciated. and just so i dont <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> anyone off... i have googled this for about 3 hours now with no luck?

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MVC Help with templating

Post by Christopher »

Code: Select all

<?php
class Template {
    
    protected $variables = array();
    protected $filename;
    
    function __construct($filename='') {
        $this->filename = $filename;
    }
 
    /** Set Variables **/
    function set($name,$value) {
        $this->variables[$name] = $value;
    }
 
    /** Display Template **/
    function render($filename='') {
        if ($filename) {
            $this->filename = $filename;
        }
        unset($filename);
        extract($this->variables); 
        ob_start();
        include $this->filename;
        $out = ob_get_clean();
        return $out;
    }
 
}
Then with an outer template like this:

Code: Select all

<html>
<head>
<title><?php echo isset($title) ? $title : ''; ?></title>
<?php echo isset($scripts) ? $scripts : ''; ?>
<?php echo isset($styles) ? $styles : ''; ?>
</head>
<body>
<div id="main_outer">
<div id="main_inner">
<div id="main_header">
<!-- menus here -->
</div>
<div id="main_content">
<?php echo isset($content) ? $content : ''; ?>
</div>
</div>
</div>
</body>
</html>
So something like:

Code: Select all

$main = new Template('main.php');
$template = new Template('foo.php');
// set tags in your template here
// then set the main template
$main->set('content', $template->render());
// output your page
echo $main->render();
(#10850)
mischievous
Forum Commoner
Posts: 71
Joined: Sun Apr 19, 2009 8:59 pm

Re: MVC Help with templating

Post by mischievous »

I appreciate your quick response arborint! Im looking into now and will post if I have any questions!

Thanks again! :D
Post Reply