CodeIgniter Forums

Full Version: How do you work with Views within your Controller?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
So I've been tinkering with CI, and I'm pretty sure I want to use it for my next project, but I've been putting together a demo site, and I'm a bit uncomfortable with how I find myself repeating my view code in my controllers.

I've created a few views out of some content that I intend to repeat a bit: 'header', 'nav', 'footer', the content was mainly broken up for organizational purposes.

I then surround the view that I'm working on in my controller, like so:

Code:
$this->load->view('header');
$this->load->view('nav');
$this->load->view('register', $data);
$this->load->view('footer');

So, I guess this is a two part question:

1) I'm typing this stuff in *every* controller I make, and it just feels like there might be a better solution for this rather than copy and pasting the general views on every controller that I make.

2) With regards to these repetitive pages, if I need to run general queries or assignments to pass in generic data to the view, that will *also* be copied into every view. (example: passing in some session data into the nav view)

I read the wiki about using multiple views on the same page, and while it told you *how*, it didn't really jump at talking about if there was a better perceived way about handling this kind of stuff.

Any advice/comments is appreciated. Smile

El Forum

[eluser]InsiteFX[/eluser]
Read this and MY_Controller in the User Guide.
CodeIgniter Base Classes Keeping it DRY

InsiteFX

El Forum

[eluser]techgnome[/eluser]
Check out my posting in this thread http://ellislab.com/forums/viewthread/173268/

In short, I let my template load all of the redundant stuff - header, navigation, footer, etc... I pass a value from the controller to the template that says what the content should be and the view loads the main content view.

-tg