Welcome Guest, Not a member yet? Register   Sign In
Templating question?
#1

[eluser]RecoilUK[/eluser]
Hi guys

Just looking for some advice really on how to best seperate different templates within CodeIgniter.

I know you can create as many views and controllers as you need and you can call these when needed, but I have a question regarding this.

Say I create a header view and controller how can I prevent this controller from being called directly from the URL, is there a built in method for doing this?

Thanks
#2

[eluser]kmanlove[/eluser]
Quote:how can I prevent this controller from being called directly from the URL, is there a built in method for doing this?

I'm not entirely sure what you mean by this. Are you talking about a private method or some sort of authentication control? The first one is discussed in the user guide, and the forum is filled with authentication control posts.

Quote:Just looking for some advice really on how to best seperate different templates within CodeIgniter.

Even with this, I'll need some clarification. Separate like in your file structure? The way they're being served up? You mentioned a header template; are you referring to splitting up a view into different files?

Keith
#3

[eluser]RecoilUK[/eluser]
Hi

No, not authentication at this point.

Yes, splitting a view up into different files, and some of these having there own controller.

So staying on the header example, it displays a basic header, unless a condition is met, then it displays the same header but with more information, etc.

To do this I want the header to have its own controller, otherwise I have to repeat the code, but I dont want the header to be displayed directly from a URL.

For example www.example.com/header

This should return a 404.

Thanks for the reply.
#4

[eluser]Sean Gates[/eluser]
Your header view should not need its own controller. You should be able to pass it the information it needs as it is loaded. A la:

Code:
$this->load->view("my_header_view", $data);

... where $data is an array of data, scaled any way you like. So, if you want to send it other views you could do something like:

Code:
$data['header_include'] = $this->load->view("my_header_include_view", NULL, TRUE);
$this->load->view("my_header_view", $data);
#5

[eluser]RecoilUK[/eluser]
Hi

Thanks for the reply.

Code:
$this->load_>view("my_header_view", $header_data);

This will certainly be used, my question was regarding the building of $header_data, how can I seperate this into a seperate file and call this file from everypage that needs it.

I have been reading the user guide, and I think this may be what i,m after.

Code:
$this->load->file('module/header', false);

So long as I keep this file procedural, I shouldnt run into any problems, would this be correct?

Thanks
#6

[eluser]Sean Gates[/eluser]
So, to your original question regarding a "header controller": you should only use controllers as the logic of the site, and not as a way to display content. If you need to display anything, it should be done in a view.

There are a couple other strategies:

1) Create a MY_Controller that is the base controller for the class and do the header logic in the __construct()
2) If using PHP5 declare the method of a controller "private": private function _header(){ ... } ... and call it whenever you need the header to display: _header();

Let me know if these make sense. I've done it many different ways.




Theme © iAndrew 2016 - Forum software by © MyBB