Welcome Guest, Not a member yet? Register   Sign In
Master templating with CI
#1

[eluser]gnomixa[/eluser]
Hi guys (and gals)!
I am a noob to CI, I read the user documentation and did the beginner tutorial to get the basics.

However, I still have a question before I proceed with CI (i am still evaluating it for my next project).
I am sure this has been asked before, but after an hour on google I couldn't find an answer to this.

I want an easy and logical way to maintain the same look for an entire web site.
So far the only way I have seen it being done is something like this:
<?php
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('content');
$this->load->view('footer');
?>

from my understanding this means I have to put this code in EVERY controller.

I was wondering if there is a way to maintain the same look on the site w/ minimal code repetition. Perhaps derive a my_controller that will encapsulate it...

CI experts, what's the best way?

Thanks in advance
#2

[eluser]umefarooq[/eluser]
hi yes there is way just have look on this template library it will solve you problem here is the link

http://www.williamsconcepts.com/ci/codei...late/?v141
#3

[eluser]Armchair Samurai[/eluser]
While template libraries are nice, I find they are often overkill for many projects. If you don't need overly complex rules for templating, try something like this:

Create a view file called something like template.php, which will contain code something like this:
Code:
<?php

$this->load->view(empty($head) ? 'includes/header' : 'includes/'.$head);
$this->load->view(empty($side) ? 'includes/sidebar' : 'includes/'.$side);

?>
<div id="content">
&lt;?php $this->load->view($content);?&gt;
</div>
&lt;?php

$this->load->view(empty($foot) ? 'includes/footer' : 'includes/'.$foot);
Then in your controller, when you call a view:
Code:
$this->load->vars(array(
    'content' => 'foo',
    'var1' => 'example 1',
    'var1' => 'example 2'
));
$this->load->view('template');
... and so on.
#4

[eluser]gnomixa[/eluser]
so loading views inside other views is acceptable?
#5

[eluser]Armchair Samurai[/eluser]
If it's good enough for Rick Ellis, it's good enough for me.
#6

[eluser]gnomixa[/eluser]
This method reminds me of the times when my server code was embedded in my html. Of course, CI is way more organized, but I would still rather separate my code as much as I can. I don't mind echoing variables, but is there a way to avoid calling functions in my views? (and loading another view is calling a function)


I am interested to know what options I have as far as templating goes.....
#7

[eluser]The Wizard[/eluser]
hello, i use a very similear approach.
i have master-.... php's which load the "header" "main" and "footer".
just like you explained it, and off course, they do it for which theme
is actually loaded.
#8

[eluser]mello.capinpin[/eluser]
check this simple template i wrote.. might help you.

http://ellislab.com/forums/viewthread/94035/
#9

[eluser]xwero[/eluser]
I've written a small tutorial to reduce the loading of views in the controller functions here
#10

[eluser]gyo[/eluser]
You can load the different views directly in your master template using the same code:

&lt;?=$this->load->view('header')?&gt;

I know short tags are deprecated but I'll use them as long as they can be enabled in the php.ini... Wink




Theme © iAndrew 2016 - Forum software by © MyBB