Welcome Guest, Not a member yet? Register   Sign In
Creating View from Multiple Files.
#1

[eluser]uris[/eluser]
I am a newbie here. Most of my development I have done with Smarty, but I discovered CI and I like it alot, so will be switching over. I have been playing with it and have a question relating to views. In Smarty, I normally divide my views into various files, for instance, my headers would be under header.tpl, footers under footer.tpl, navigations under navigation.tpl and menus under menu1.tpl, menu2.tpl, etc. And they would all be assembled by a main.tpl. On my main.tpl I would have something like this:

Code:
{include file = $header}
   {include file = $navigation}
   {include file = $menu}
   {include file = $content}
   {include file = $footer}

That way, if I ever need to make a change to the header or to a particular menu or navigation, I don't have to edit all my views, only one file and the change would take effect site-wide.

Is there any way to replicate this on CI??
#2

[eluser]Unknown[/eluser]
[quote author="uris" date="1183968155"]

Is there any way to replicate this on CI??[/quote]

In controller:

Code:
$this->load->view('container',$data);

in view file container.php:

Code:
$this->load->view('header');
$this->load->view('body');
$this->load->view('footer');

...etc...

Any variables for any of the included views can be passed in the data array.
#3

[eluser]Rick Jolly[/eluser]
More than you wanted to know about embedding views within views:

http://ellislab.com/forums/viewthread/44916/

You might want to check out the 4th post by Craig_ which is a slightly different approach than that outlined by ztar.
#4

[eluser]esra[/eluser]
You can also use Smarty with CI if you prefer to use a template engine. There are several threads. One is below.

http://ellislab.com/forums/viewthread/44719/

I believe that Mohrt, one of the Smarty maintainers, used CI and Smarty to build motortopia.com.
#5

[eluser]uris[/eluser]
Thank you very much !
#6

[eluser]ballen[/eluser]
I thoroughly recommend YATS as a templating system, very useful libary.
#8

[eluser]uris[/eluser]
Thanks alot, I like coolfactor's solution. Smile
#9

[eluser]Mirage[/eluser]
I second coolfactor's approach as the most flexible solution.

CI's views are really more like templates, albeit rather smart ones. I'm just putting the finishing touches on a multi-lingual site in which I used a modified version of coolfactor's view. I'm really digging the abstraction it provides - it keeps my controller more organized and has exposed a lot of DRY problems with my views. Which is a very good thing as I keep digging toward a CI based CMS for my sites.

Cheers!
#10

[eluser]sophistry[/eluser]
I've added this question to the FAQ at the wiki.

Did you know there is now a FAQ at the wiki? Let's get some more stuff up there - right now it is pretty bare.




Theme © iAndrew 2016 - Forum software by © MyBB