• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is'this correct? (Load content from a view)

#1
[eluser]ariok[/eluser]
Hi all i got some problems with a simple layout:

I have a controller called "home":

Code:
<?    
class Home extends Controller {
    function index(){
        
    }
    
    function Home() {    
        parent::Controller();
        $this->load->helper('url');
        $this->load->view("main_tpl");
    }
    
    function content()    
    {
        $this->load->view("home_view");
    }    

}
?>

And a view called main_tpl.php..where i have the main template... in center of this template there's this code
Code:
.
        .
        .
        <div id="content">
        &lt;?=$this->content()?&gt;
    </div>
        .
        .
        .


Now... when i load myurl/index.php/home i obtain this error:
Call to undefined method CI_Loader::content() in...etc etc

with $this->content() i want to load function content defined in home.php ..is it correct?? where is my error...

P.s
I test this in my local machine without problem.. putting this code on a unix server i got the error before explained.

#2
[eluser]chobo[/eluser]
Hi, I'm not sure what's wrong with your above example, but you could try setting the view in the controller to a variable and passing that to the view.

Code:
function content()    
    {
        $data['home'] = $this->load->view('home_view', '', true);
    }


Code:
In view display view like this:

&lt;?php echo $home; ?&gt;

#3
[eluser]CI TaMeR[/eluser]
You can't call in the view page a function from the controller

Code:
function Home() {    
        parent::Controller();
        $this->load->helper('url');
        this->content();
        $this->load->view('main_tpl', $this->tpl);
    }
    
    function content()    
    {
        $this->tpl['content'] = $this->load->view("home_view");
    }

Now you can access the data from contect via $content in the view.

#4
[eluser]ariok[/eluser]
ok! thank you for your answers i'll try with this method.

#5
[eluser]ariok[/eluser]
Ok it's perfect.. but now i have another problem Sad
This is an overview of my situation :

-A main layout where is defined my site structure and where contents are loaded
-Suppose that i have only 2 sections Home and Contacts
-i defined 2 controller and 2 views for home and contact as described in previous posts and
1 view for main_layout where i print a $content variable value..

here the problem.. i need to print an information from a DB, and this info must be visible in all pages... but i think that it's not correct to do something like this

Code:
function Home() {    
        parent::Controller();
        
        //HERE--------------------------
        $data['content'] = $this->load->view("home_view","",true);
        $this->load->model('Info');
        $data['information'] = $this->Info->get_info();
        //------------------------------
        $this->load->view("main_tpl",$data);
        
    }

And do the same for contacts

Code:
function Contacts() {    
        parent::Controller();
        
        //HERE--------------------------
        $data['content'] = $this->load->view("contacts_view","",true);
        $this->load->model('Info');
        $data['information'] = $this->Info->get_info();
        //------------------------------
        $this->load->view("main_tpl",$data);
        
    }


In this mode.. i need to repeat
Code:
$this->load->model('Info');
$data['information'] = $this->Info->get_info();
in all controller... and get this data in main layout ...

is there an better way without repeat this info in all controllers?????

#6
[eluser]deviant[/eluser]
Is it good practice to load a view in the Controller constructor?

#7
[eluser]ariok[/eluser]
uhm.. i don't know really .. my problem now is another Big Grin

#8
[eluser]deviant[/eluser]
Well, you could either extend the Controller class and put that stuff in the constructor or use a hook. The only problem with a hook is that you would probably want to use a post-controller-constructor hook. Since you are doing all that stuff in the constructor I don't think that would work, as that hook is executed after all that code. A pre-controller hook wouldn't have access to the Loader for your model and so is equally useless for your purposes.

In extending the Controller you would have to change the code slightly, like loading the data thats going into $data["information"] into (for example) $this->information instead, so that it can be accessed by the child class.

#9
[eluser]ariok[/eluser]
Ok ! and so to extend controller is better if child of "my_controller" load view from index function and not from constructor ?

#10
[eluser]ariok[/eluser]
ehm.... ok .. now i could extend controller and to something like this
Code:
class New_controller extends Controller{
some code where i define my information data... $this->information.. etc
}

and use this in this manner?

Code:
class Home extends New_controller {
?????
}

I'm confuse.... Tongue


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.