Welcome Guest, Not a member yet? Register   Sign In
The idea of templates for header/footer
#1

[eluser]Unknown[/eluser]
Hi,

While I was continuing with my tutorials and other stuff I came across an idea to create a template view that would load a couple of other views like header, footer and the main content. I like the idea as it would help to include my navigation menu on most of the pages without the need to load it explicitly in every place. The code of the view in the tutorial is something like that:

Quote:<?php $this->load->view('templates/header'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('templates/footer'); ?>

Now obviously when loading this view (let's call it template) I need to call $this->load->view('template', $data) and before that I need to pass the name of the main content view to $data, like for example $data['main_content'] = 'someview'.

This is more or less where it ends in the tutorial as for the view part. Now what I need to do is obviously pass some data to the main content view. If I understand well, in order to do that I would have to pass not only the name of the view that needs to be displayed in main_content but also its data. Now I have:

$data['main_content'] = 'someview';
$data['main_content_data'] = $someotherdata

and I need to modify the original view template.php to something like:

Quote:<?php $this->load->view('templates/header'); ?>
<?php $this->load->view($main_content, $main_content_data); ?>
<?php $this->load->view('templates/footer'); ?>

I guess that building an application that would have more or less constant menus and other stuff in place on every screen and just main content changing is a quite common task. That's why I would want to ask if the way provided above is the right way to do it or there are maybe other ideas on how to handle it?

Best regards,
Kuba
#2

[eluser]noideawhattotypehere[/eluser]
1.Extend CI_Loader
2.Write new function view(), so its like:
Code:
public function view($yourview) {
parent::view(templates/header);
parent::view($yourview);
parent::view(templates/footer);
}
win everytime you call $this->load->view('whatever');
#3

[eluser]Unknown[/eluser]
Thanks a lot for the tip. I'll think about it but I believe this one won't fit to my needs very well. In such a case I will have the header/footer on literally every single view in the application and I'll lose control over it. Plus, if I understand well, with extending the CI_Loader I won't be able to load more than one view from a controller.
#4

[eluser]duartix[/eluser]
It's really interesting how unconnected people solving a simple problem will most of the times come up with the same solution...
Must be the right one! Smile

Can't see why it doesn't fit your needs (even if you need to pass data to the header and footer). I've just built an application with the very same requisites and this approach worked fine. If you need to pass data to the header and footer, you can address the data the same way you addressed the views, just fill it up and pass it.


Code:
public function view($yourview) {
parent::view(templates/header);
parent::view($yourview, $data);
parent::view(templates/footer);
}


{
$data["header"] = $hData;
$data["footer"] = $hFooter;
$data["view"] = $yourData;

$this.view("MyView", $data);
}
#5

[eluser]noideawhattotypehere[/eluser]
[quote author="kubakk" date="1382263127"]Thanks a lot for the tip. I'll think about it but I believe this one won't fit to my needs very well. In such a case I will have the header/footer on literally every single view in the application and I'll lose control over it. Plus, if I understand well, with extending the CI_Loader I won't be able to load more than one view from a controller.[/quote]

You dont really need to call it view() if you need the old one tho.




Theme © iAndrew 2016 - Forum software by © MyBB