Welcome Guest, Not a member yet? Register   Sign In
rudimentary template page chunks in my views, comments welcome
#11

[eluser]Aea[/eluser]
Okay, so this might be more confusing then you probably want, but it's important, see the code I wrote below to gain a better understanding of class scopes, but basically in this case all you want to do is make $CI accessible everywhere in your class, now the proper method to do this would be...

Code:
class Template
{
    protected $CI; // This is optional but recommended

    function __construct()
    {
        $this->CI =& get_instance();
    }
    
    function show()
    {
        $this->CI->something...
    }
}

Now let's analyze what we're doing here, we're declaring that there will be a protected (i.e. only accessible intraclass and to children), then we're defining it in __construct. Now because we're in a method, we want to tell the class to retrieve that variable for us (our local scope would be within the method, and CI only exists in the class scope), so we use $this->CI to access it and set it as a reference to the master CI class. Then to use it within any method you need to do the same $this->CI, since CI is a class scope variable. You can skip the declaration if you want, but it is good practice to keep limits on variable scope, but if you haven't begun OO programming that will be entirely confusing.

Here's some quick sample code I hacked up to give you an idea about variable scopes.

Code:
<?php

class Template
{

    public         $classScopeVariablePublicHeader = 'waffle';    // public keyword is optional
    private        $classScopeVariablePrivate         = 'crepes';    // private keyword is mandatory if you want to make it private
    protected    $classScopeVariableProtected     = 'blinz';    // same deal here    

    public function __construct()
    {
        $this->classScopeVariablePublicConstructor = 'pancakes';
    }
    
    public function method()
    {
        echo get_class($this).' method()'.PHP_EOL;
        
        echo     "Public:\t\t".$this->classScopeVariablePublicHeader.PHP_EOL.
                "Public:\t\t".$this->classScopeVariablePublicConstructor.PHP_EOL.
                "Private:\t".$this->classScopeVariablePrivate."\t\t // You can see me only in the original class".PHP_EOL.
                "Protected:\t".$this->classScopeVariableProtected."\t\t // You can see me only in the original & child class".PHP_EOL.PHP_EOL;
                
        $methodScopeVariable = 'Something Else';
    }

}

class FancyTemplate extends Template
{
    public function method()
    {
        echo get_class($this).' method()'.PHP_EOL;
        
        echo     "Public:\t\t".$this->classScopeVariablePublicHeader.PHP_EOL.
                "Public:\t\t".$this->classScopeVariablePublicConstructor.PHP_EOL.
                "Private:\t".$this->classScopeVariablePrivate."\t\t // You can see me only in the original class".PHP_EOL.
                "Protected:\t".$this->classScopeVariableProtected."\t\t // You can see me only in the original & child class".PHP_EOL.PHP_EOL;
    }
}


$Template         = new Template;
$FancyTemplate    = new FancyTemplate;

$Template->method();
$FancyTemplate->method();

echo 'Now For main Access (on Template, but FancyTemplate will be the same)...'.PHP_EOL;
echo     "Public:\t\t".$Template->classScopeVariablePublicHeader.PHP_EOL.
        "Public:\t\t".$Template->classScopeVariablePublicConstructor.PHP_EOL.
        "Private:\t"./*$Template->classScopeVariablePrivate.*/"\t\t // You can see me only in the original class (This will cause a fatal error)".PHP_EOL.
        "Protected:\t"./*$Template->classScopeVariableProtected.*/"\t\t // You can see me only in the original & child class (This will cause a fatal error)".PHP_EOL.
        "Method Scope:\t".$Template->methodScopeVariable."\t\t // You can't access me outside of my method! (This will cause notices)".PHP_EOL.PHP_EOL
        ;



echo PHP_EOL;

Code:
Template method()
Public:        waffle
Public:        pancakes
Private:    crepes         // You can see me only in the original class
Protected:    blinz         // You can see me only in the original & child class

FancyTemplate method()
Public:        waffle
Public:        pancakes
Private:             // You can see me only in the original class
Protected:    blinz         // You can see me only in the original & child class

Now For main Access (on Template, but FancyTemplate will be the same)...
Public:        waffle
Public:        pancakes
Private:             // You can see me only in the original class (This will cause a fatal error)
Protected:             // You can see me only in the original & child class (This will cause a fatal error)
Method Scope:             // You can't access me outside of my method! (This will cause notices)
#12

[eluser]Colin Williams[/eluser]
As the author of a Template library, I've actually, more and more, been abandoning the idea altogether. Employing a new base controller or providing your own controller class entirely usually opens up enough flexibility with "global" stuff that you lose the need to have a library on top of views.
#13

[eluser]Flying Fish[/eluser]
Thanks for the feedback.

I think all I really need to do is declare a few variables that I can use anywhere in view files.

There has to be a simple way of doing this, but I'm just not familiar enough with OOP and what's really going on under the hood to make it all work out.

Extending the Controller sounds like a good solution. For now, I may just load views from within my views, as ugly as that sounds.
#14

[eluser]Colin Williams[/eluser]
Quote:I think all I really need to do is declare a few variables that I can use anywhere in view files.

In the base controller constructor (or on its behalf) do a $this->load->vars().
#15

[eluser]Flying Fish[/eluser]
could I trouble you for an example?
#16

[eluser]Colin Williams[/eluser]
Code:
class MY_Controller extends Controller {

   function MY_Controller()
   {
      parent::Controller();
      $this->load->vars('header', $this->load->view('header', NULL, TRUE));
      $this->load->vars('footer', $this->load->view('footer', NULL, TRUE));
      $menu = $this->menu_model->get();
      $this->load->vars('menu', $menu);
   }

}

// Just a few ideas.
#17

[eluser]Flying Fish[/eluser]
I'll give a shot implementing this before I give up :-)
#18

[eluser]Flying Fish[/eluser]
getting this error

Quote:Fatal error: Call to undefined method MY_Controller::my_controller() in /Applications/MAMP/htdocs/staging2/system/application/controllers/welcome.php on line 16

here the MY_Controller File

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
    The MY_Controller Class allows me to add functionally to CI's Controller Class
    
    I basically just use it add some 'global' vars so I can include views as page chunks
    
    Just make sure any controller that you need this extends MY_Controller and not Controller
*/

class MY_Controller extends Controller {

   function __construct()
   {
      parent::Controller();
      
      $this->load->vars('header', $this->load->view('_inc/head', '', TRUE));
      $this->load->vars('footer', $this->load->view('_inc/masthead', '', TRUE));
      
     // $menu = $this->menu_model->get();
      //$this->load->vars('menu', $menu);
   }

}

and here's the welcome controller that calls it

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*

    The Welcome class serves as the home page.
    Users can choose to 'Get Started' which will walk them through the process of creating their account and ordering itmes
    Or they can choose to 'Login' if they have already ordered some items

*/


class Welcome extends MY_Controller {

    function __construct()
    {
        parent::MY_Controller();
        
        // Customize the tags that wrap arround the form validation error messages
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $this->load->library('template');
    }


happen to see anything I'm doing wrong?
#19

[eluser]Flying Fish[/eluser]
ok found the problem

I guess I have to do this in the MY_Controller Class

[code]
class MY_Controller extends Controller {

function MY_Controller()
{
parent::Controller();
[code]

rather than
[code]
class MY_Controller extends Controller {

function __construct()
{
parent::Controller();
[code]

now I just need to figure out how to display data from $this->load->vars in my view files
#20

[eluser]MEM[/eluser]
Maybe because you are running php 4 or something?

I believe in php5 you can use __construct() or the same name for the method and the class, to define a constructor.

no?

Something like:
Code:
class MY_Controller extends Controller
{

  function __construct()
  {
    parent::__construct();
  }
}

Will work fine yes?


Regards,
Márcio




Theme © iAndrew 2016 - Forum software by © MyBB