Trying to make a template go into a master template.

Discussion in 'Web Design and Development' started by Cabbit, Jan 1, 2010.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    Hey folks what i am trying to do today since i have my controller action class sorted(thanks for the help getting started there guys). I am now working on a templater more specifically the template class works in for the basic bit but i want to make it put the view template into the layout template.

    template.php
    PHP:
    Class CabbitTemplate 

        private 
    $CabbitRegistry
        private 
    $vars = array(); 
        function 
    __construct($CabbitRegistry
        { 
            
    $this->CabbitRegistry $CabbitRegistry
        } 
        public function 
    __set($index$value
        { 
            
    $this->vars[$index] = $value
        } 
         
        public function 
    layout() 
        { 
            
    $viewPath __SITE_PATH '/application/layout' '/layout.php'

            if (
    file_exists($viewPath) == false
            { 
                throw new 
    Exception('Template not found in '$viewPath); 
                return 
    false
            } 
            return 
    $viewPath;                
        } 
         

        function 
    show($name
        { 
            
    $viewPath __SITE_PATH '/application/views' '/' $name '.php'

            if (
    file_exists($viewPath) == false
            { 
                throw new 
    Exception('Template not found in '$viewPath); 
                return 
    false
            } 

            
    // Load variables 
            
    foreach ($this->vars as $key => $value
            { 
                $
    $key $value
            } 
            include 
    $this->layout($viewPath);                
        } 

    }
    indexcontroller.php
    PHP:
    class inderController Extends CabbitControllerAction 

            
    $this->CabbitRegistry->CabbitTemplate->show('/index/index'); 
    /view/index/index.php
    PHP:
    <h1><?php echo $welcome?></h1> 
    <p><?php echo $status?></p> 
    <?php echo $user?> 
    <br /> 
    <a href='/user/logout'>Logout</a>
    /layout/layout.php
    PHP:
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
    </head> 
    <body> 
    <div id="content"> 
        <?php echo $this->CabbitRegistry->CabbitTemplate->layout(); ?> 
    </div> 
    </body> 
    </html>
    So far my best effort outputs
    HTML:
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
    </head> 
    <body> 
    <div id="content"> 
        /Users/jerryroy/Sites/Cabbit 1.0/application/layout/layout.php</div> 
    </body> 
    </html>
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    I'm looking at this fairly quick and with a slight headache, but I believe what you want is,
    PHP:
    <div id="content"> 
        <?php include ($this->CabbitRegistry->CabbitTemplate->layout()); ?> 
    </div>
    since you're returning a file path from layout().
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    Oh a full source code for what i have developed so far is here. Its not very big.
    Also a note if you download it /application/bootstrap.php does nothing just now.

    From looking at how the zend framework does it i need to create a bootstrap class that is a helper to my controller class so that i would define a master template in this bootstrap class that uses the template class as a helper by shoving in the template data into the bootstrap master template.

    How this will work i am not yet sure, i am quite happy to have made a basic mvc framework as it is it is not getting wonderfully complex ^_^

    So i will be working the bootstrap class using the template class as i base i would think, then i would need some way to make a master and slave relationship between the bootstrap that loads the layout and the template that loads the individual helper template files.

    So it should be something like user opens page > cabbit calls index controller > cabbit calls > bootstrap > index controller populates the template class > template class comes back > cabbit puts the data into the layout for the user to see.

    This is the original template file.
    PHP:
    Class CabbitTemplate 
    {
        private 
    $Cabbit;
        private 
    $vars = array();
        function 
    __construct($Cabbit
        {
            
    $this->Cabbit $Cabbit;
        }
        public function 
    __set($index$value)
         {
            
    $this->vars[$index] = $value;
         }    

        function 
    show($name
        {
            
    $viewPath __SITE_PATH '/application/views' '/' $name '.php';

            if (
    file_exists($viewPath) == false)
            {
                throw new 
    Exception('Template not found in '$viewPath);
                return 
    false;
            }

            
    // Load variables
            
    foreach ($this->vars as $key => $value)
            {
                $
    $key $value;
            }
            include 
    $viewPath;               
        }
    }
     
  4. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #4
    Oh why oh why do i think the most difficult route is the best. This works out terrifically, only change i would make is in the future is the ability to select layouts.

    template class
    PHP:
    /*
     * Cabbit 1.0
     * Licence: GPLv2
     * CabbitTemplate v0.1
     */

    Class CabbitTemplate 
    {
        private 
    $Cabbit;
        private 
    $vars = array();
        function 
    __construct($Cabbit
        {
            
    $this->Cabbit $Cabbit;
        }
        public function 
    __set($index$value)
         {
            
    $this->vars[$index] = $value;
         }    

        function 
    show($name
        {
            
    $viewPath __SITE_PATH '/application/views' '/' $name '.php';
            
    $layoutPath __SITE_PATH '/application/layout/layout.php';

            if (
    file_exists($viewPath) == false)
            {
                throw new 
    Exception('Template not found in '$viewPath);
                return 
    false;
            }

            
    // Load variables
            
    foreach ($this->vars as $key => $value)
            {
                $
    $key $value;
            }
            
    $view $viewPath;
            include 
    $layoutPath;               
        }
    }
    layout
    PHP:
    <html>
    <head></head>
    <body>
        <h1>Cabbit 1.0</h1>
        <?php include $view;?>
    </body>
    </html>
     

Share This Page