Welcome Guest, Not a member yet? Register   Sign In
Problem with my controller
#1

[eluser]Crabby[/eluser]
Hi guys

Any ideas why this is coming out blank, the title shows ok but the seperate views are coming out blank??

Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $template['title']   = "Welcome";
        $template['header']  = $this->load->view('header');
        $template['content'] = $this->load->view('home');
        
        $this->load->view('template', $template);
    }
}


Any help help would be much appreciated

Regards

Crabby
#2

[eluser]deviant[/eluser]
As far as I can see you have made the same 2 mistakes in both of your first view load calls. Firstly you will need to pass an array to them, even if its empty. Secondly you need to tell the Loader to return those views instead of trying to print them.
#3

[eluser]Crabby[/eluser]
Hi

Perfect, thank you for your reply, my apologies I haven't had the time to read the documentation fully.

Just out of interest would you say this is the best way to implement multiple views IE the template components are added in each controller or is best to have a seperate controller to handle this eg template controller?

Thanks again

Crabby
#4

[eluser]Michael Wales[/eluser]
The view() method has a third BOOLEAN parameter that allows you to either return the value or send it to the browser (the default). By not definining this - you are trying to send multiple view to the browser from the same controller.

Try the following code:
Code:
class Home extends Controller {
  function Home() {
    parent::Controller();
  }

  function index() {
    $template['title'] = 'Welcome';
    $template['header'] = $this->load->view('header', , TRUE);
    $template['content'] = $this->load->view('home', , TRUE);
    $this->load->view('template', $template);
  }
}
#5

[eluser]Crabby[/eluser]
Hi walesmd

Thanks for your reply, I managed to figure out what I was doing wrong after reading the documentation.

My second point was really from a design point of view. Does it make sense to build up the different parts of the page (header,latest news, footer) in each controller?

Thanks again

Crabby
#6

[eluser]Michael Wales[/eluser]
I like to keep my Controllers as lean as possible, so I load my header/footer from the individual views. It's up to you really though.
#7

[eluser]Crabby[/eluser]
As includes?

Thanks for the reply
#8

[eluser]deviant[/eluser]
You can load them using the CI Loader seeing as when your view is being loaded it is just being included in the Controller. This would be your view file:

Code:
<?php $this->load->view("header", ''); ?>

rest of your view

<?php $this->load->view("footer", ''); ?>

Of course including them using the PHP include function would work as well but doesn't seem as tidy to me.
#9

[eluser]Crabby[/eluser]
Hi deviant

Thanks for your reply.

What if I wanted to include the latest news articles consistantly on every page would each view file fetch the data from the news model?

Cheers

Crabby




Theme © iAndrew 2016 - Forum software by © MyBB