CodeIgniter Forums
Managing views, how do you do it? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Managing views, how do you do it? (/thread-22402.html)



Managing views, how do you do it? - El Forum - 09-08-2009

[eluser]eightamrock[/eluser]
Hello All,

I'm new to CI, but I am familiar with MVC architectures such as ROR and Cake.

The question I have always struggled with is how to properly manage views. Do you construct your views from header and footer files, or do you have a view for every page/function that feeds dynamic data?

I am used to one central view that includes a switch function that pulls the proper layout for a page. This makes managing views easy, but I am struggling with how to do something similar in CI. I know ROR offers a default layout that will pass in data from your function automatically.

I'm open to suggestion or just a healthy debate on the best way to do it.

-Sam


Managing views, how do you do it? - El Forum - 09-08-2009

[eluser]John_Betong[/eluser]
[quote author="eightamrock" date="1252451735"]Hello All,

I'm new to CI, but I am familiar with MVC architectures such as ROR and Cake.

The question I have always struggled with is how to properly manage views. Do you construct your views from header and footer files, or do you have a view for every page/function that feeds dynamic data?

I am used to one central view that includes a switch function that pulls the proper layout for a page. This makes managing views easy, but I am struggling with how to do something similar in CI. I know ROR offers a default layout that will pass in data from your function automatically.

I'm open to suggestion or just a healthy debate on the best way to do it.

-Sam[/quote]
 
 
Search this forum for "Partial views" and all will be revealed Smile
 
 
 


Managing views, how do you do it? - El Forum - 09-08-2009

[eluser]BrianDHall[/eluser]
Personally I use individual views for things relating to my basic site template, like header, footer, leftnav, rightnav. I initialize these into properties I set in my controller in the constructor, such as:

Code:
class DefaultController extends Controller {

    var $header, $leftnav, $content, $rightnav, $footer, data = '';
    
    function DefaultController()
    {

        parent::Controller();

        $this->data['header'] = &$this->header;
        $this->data['leftnav'] = &$this->leftnav;
        $this->data['content'] = &$this->content;
        $this->data['rightnav'] = &$this->rightnav;
        $this->data['footer'] = &$this->footer;
                $this->header = $this->load->view('header', $this->data, true);
        $this->leftnav = $this->load->view('leftnav', $this->data, true);
        $this->rightnav = $this->load->view('rightnav', $this->data, true);
        $this->footer = $this->load->view('footer', $this->data, true);
         }
}

Then in my pages I load their specific view, such as 'frontpage', load it into $this->content, and then load the default view which prints out the values for the predefined elements in their proper HTML placements.

I further organize by naming views with '_form' that are just forms, and then I stick '_success' and '_failure' as appropriate so I know what they are supposed to be used for - as needed, of course.

I like the flexibility and automation it provides, and it allows new pages to be built by just using 2 lines of code:

Code:
function index()
    {
        $this->content = $this->load->view('frontpage', $this->data, true);

        $this->load->view('defaultview', $this->data);
    }

And if I need 1 function for some reason to totally change (or just add to) any of the navigation or page elements its easy and lightweight.

And it works, which I like the most of all Wink


Managing views, how do you do it? - El Forum - 09-08-2009

[eluser]bretticus[/eluser]
I actually have designers edit my views from time to time so I really stick to the to basics. Heaven forbid that view can't be clearly edited in Dreamweaver Design mode! Smile


Managing views, how do you do it? - El Forum - 09-09-2009

[eluser]eightamrock[/eluser]
wow some really great feedback. I searched Partial views and got a lot of great data.

BrianD, I really like your method. I am used to something similar in my own app designs. I would construct pages through includes so if the nav changed or anything like that, I would only have to change it in one place.

With an MVC architecture, I guess it makes that even easier!

Bretticus, I know the feeling. I have worked in a custom website shop for 5 years, designers, copywriters, SEO specialists etc. If they can't GUI it, it cant be done...

Thanks!


Managing views, how do you do it? - El Forum - 09-09-2009

[eluser]bretticus[/eluser]
Be sure to checkout this simple layout library.

Here's a way you can use hooks to generate a base layout.


Managing views, how do you do it? - El Forum - 09-09-2009

[eluser]eightamrock[/eluser]
Thanks! This is great, very similar in design to what BrianD was explaining.