What actually goes in YOUR header? |
I understand the concept of having a header to stop duplicate code. What I have so far though, I'm not sure what will be duplicated in a header. The title of the page will be different each time, the js files and css files will be different each time. So what do you reuse? And how to you deal with things like css and js files that vary?
Also, somewhat sort of related. Is it ok to load a view in another view like so Code: <!DOCTYPE html>
Why not try put this in your controller, $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); You can't $this->load->view('blabla'); in another view.
Keep calm.
(01-27-2016, 11:15 AM)arma7x Wrote: Why not try put this in your controller, $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); You can't $this->load->view('blabla'); in another view. Not that I've tried it (I use Smarty for this kind of thing) but I think he can do this kind of logic as long as he gets the CI super object instance with get_instance() function, somewhere on the top section of his view. I didn't test that though, and probably is not that simple. But the logic is that. Just saying... \_(-.-)_/
Best regards,
José Postiga Senior Backend Developer
Why getting get_instance() in view, it already has CI super object.
Keep calm.
Codeigniter Template example
http://forum.codeigniter.com/thread-6379...#pid326426 same example that is also calling a model in the template for dynamic headers http://stackoverflow.com/questions/34909...4#34911214
"Why not try put this in your controller, $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); You can't $this->load->view('blabla'); in another view."
Because I don't want to be calling 3 or 4 views from my controller over and over again. And you can load a view from another view. At least it works when I try it. I just wasn't sure if it's a good idea. Thanks for the links Cartalot. (01-28-2016, 01:43 AM)DreamOfSleeping Wrote: "Why not try put this in your controller, $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); You can't $this->load->view('blabla'); in another view." It's perfectly fine to load a view from another view. It works, and the documentation shows an example, if I remember right. I happen to use the same header. If something in it is variable, I just pass the value to it in a variable. So every view of mine goes like this. PHP Code: $this->load->view('header'); I can even have subviews. For example, you might have some kind of table, and below it a form that can be an add, edit, or delete form. I put the table in the view, and from that view, I load the desired subview, which is the form that adds, edits, or deletes.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Thanks Robert. I think your example is what I'm going to go with. Looking up a lot of articles about this subject, and some people seem to frown upon the loading the header inside another view each time. Mainly because they class it as dupilicate code. But I think repeating one line of code at the top and bottom of a view is hardly anything to worry about.
Today I wasted my time making my own way of templating. This is what I came up with. I'm not going to use it though. Code: <?php (01-28-2016, 12:51 PM)DreamOfSleeping Wrote: Thanks Robert. I think your example is what I'm going to go with. Looking up a lot of articles about this subject, and some people seem to frown upon the loading the header inside another view each time. Mainly because they class it as dupilicate code. But I think repeating one line of code at the top and bottom of a view is hardly anything to worry about.You bet, man, and you're right. DRY -- don't repeat yourself -- is a good idea but not a religious commandment. Besides, let's look at the different ways of doing this (just the headers and footers views). You could include the header and footer code in each view. Result? Massive code duplication. You could load header and footer from the controller. PHP Code: $this->load->view('header'); You could load header and footer from the body view itself. Result? The least code duplication of all three options. You can forget entirely about the header and footer once you put it in your views. Is there some code duplication? Of course. It's not possible to eliminate code duplication entirely. I haven't paid attention to templating, but here's how I do it. In the controller, PHP Code: public function index() Over in the view searches_view, we have this (full code), PHP Code: <?php $this->load->view('header'); ?> If you'd like to see the entire application, which is not complex but which uses a few advanced CodeIgniter features, like driver libraries, download the whole thing at https://github.com/RobertHallsey/search-scraper
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Thanks. I will check out your application. It will be useful to see a full working example like that.
|
Welcome Guest, Not a member yet? Register Sign In |