Welcome Guest, Not a member yet? Register   Sign In
help with loading multiple views
#1

[eluser]DumpProgrammer[/eluser]
I am working on my first website with CI. I have done my view file template.php like this
Code:
<?php $this->load->view('newheader'); ?>
<?php $this->load->view('newnav'); ?>

<?php $this->load->view('newfeatured');?>
<?php $this->load->view('newcontent');?>
<?php $this->load->view('footer'); ?>
Which I have realised is a wrong way of loading multiple views in CI. I would like some help with moving all these view files to my controller but I am not sure on how to do this.
#2

[eluser]bretticus[/eluser]
Congratulations and welcome to CI.

I think you are using the "model" of loading a single view template that, in turn, loads another view to display the content you need. This method is a little unorthodox but it will work.

Another method is to call the views individually (just like you have them minus all the duplicate php tags) in the controller:

Code:
<?php
class whatever extends Controller
{
function someaction() {

$data = array();
$data['content'] = 'Some content';

$this->load->view('newheader');
$this->load->view('newnav');

$this->load->view('newfeatured');
$this->load->view('newcontent', $data)
$this->load->view('footer');
}
}?>


Of course, if you want to stay with the template model, you need to load that view from the controller:

Code:
<?php
class whatever extends Controller
{
function someaction() {

$data = array();
$data['contents'] = array('content'=>'Some content');
$data['view'] = 'content_view';

$this->load->view('template', $data)
}
}?>

and then in your template.php view:

Code:
<?php $this->load->view('newheader'); ?>
<?php $this->load->view('newnav'); ?>

<?php $this->load->view('newfeatured');?>
<?php $this->load->view($view, $contents);?>
<?php $this->load->view('footer'); ?>
#3

[eluser]DumpProgrammer[/eluser]
Thanks you have helped me a lot. Now I think I will be able to work with the pagination on the single view because thats where I realised I had a problem with this method of loading multiple views in template file. Here is glimpse of the new controller
Code:
class Blog extends Controller {
  function Blog(){
    parent::Controller();
    session_start();
  }
    
    function index()
    {
    $data['posts'] = array(); //we will replace soon!
    $data['cats'] = $this->MCats->getTopCategories(); //we will replace soon!
    $data['title'] = "Shout-Africa";
    $data['main'] = 'public_home';
    $data['featured'] = $this->MPosts->getAllFeaturedPosts();
    $data['post'] = $this->MPosts->getAllActivePosts();
    $this->load->vars($data);
    $this->load->view('newheader');
    $this->load->view('newnav');
    $this->load->view('newfeatured');
    $this->load->view('newcontent');
    $this->load->view('footer');

    }
Thanks
#4

[eluser]Udi[/eluser]
I heavily recommend you to read my 3 blog posts about developing CMS with CodeIgniter,
the first one: http://blog.umnet.co.il/2009/11/25/devel...er-part-1/

I will help you to start coding with the right approach.
#5

[eluser]DumpProgrammer[/eluser]
thanks I have had a look at your blog and its a good tutorial.




Theme © iAndrew 2016 - Forum software by © MyBB