Extending the Loader class (more or less) |
[eluser]Unknown[/eluser]
Hello Problem: I need to implement some sort of themes for a site I'm working on. Solution: Extend the CI_Loader class and overwrite the "view" function, to add an optional subdirectory for my views. Something like: Code: application/themes/mytheme/view.php Extending the CI_Loader class would be simple, but we have a problem: the CI_Controller class extends CI_Loader and somewhere in the code we have: Code: $this->load =& $this; //to allow $this->load->view(...) to work The solution is simple: between CI_Controller and our app controllers, we will add a new Controller class, that will overwrite the "view" function. The result will look like: CI_Loader -> CI_Controller -> MyBaseController -> App Controller. Code: <?php and an application controller: Code: <?php I hope this helps and of course, if you have better suggestions, please post them here. Peter
[eluser]Derek Allard[/eluser]
I haven't fully digested what you're writing here, but I wanted to point out that there is a "display_override" hook available (hooks docs) that might be more flexible for you, and wouldn't require any core hacking.
[eluser]Unknown[/eluser]
The display_override hook will give me access to a string that will contain all the generated output (right? ). I find it a bit harder to process that string and change the layout, than just use another view for a different layout Let's consider the following directory structure: application/view/ <-- will contain the "default" views. application/view/themes/ <-- will contain a subfolder for each theme. application/view/themes/myfancytheme/ <-- will contain all views that are different from the default one. If I don't set a theme, then the "login.php" view will be loaded from the application/view/ directory. If I set the theme to "myfancytheme", the the "login.php" view will be loaded from the application/view/themes/myfancytheme/ directory. Ofcourse, the code I posted earlier is just the rough idea, still needs checks (if the theme directory exists and so on), maybe a fallback mecanism (that will allow me to rewrite only some views) etc.
[eluser]Rick Jolly[/eluser]
The only problem with your code is that you are calling private CI core methods that aren't part of the public API (_ci_load() and _ci_object_to_array()). A couple of alternatives: Code: #1.
[eluser]voltechs[/eluser]
Hey y'all... Back with hopefully another solid Giveback to the community. I like having a "layout" for those of you familiar with Ruby On Rails, for my sites. Figured this is what the original posted was going for. I'd ultimatly love it if this could be turned into a plug-in of sorts, or some other "override" which doesn't actually "hack" the core of CI, so to all BEWARE: THIS WILL MODIFY YOUR CORE SYSTEM. Not for the faint of heart. That being said, its not a very big change, and I've taken the liberty of creating a .patch file for you all. This will only work for unix/linux systems. Run this command to patch the Loader.php file. patch -R path/to/system/libraries/Loader.php < Loader.patch I recommend you look over the patch file so you know what's going on. It should be fairly straight forward, and I commented where necessary to blend in with the rest of the file. If you don't have Unix/Linux, and haven't made any changes to Loader.php, you can simply replace it with the one I've attached. I recommend the patch if possible over replacing. This also allows for _partial.php views. You can call them from within your views and layouts. After you're patched up, make a sibling directory to views called layouts and pop in your php/html layout there. Then just define your layout in your controller by saying: $layout = "name_of_layout"; Code: class Admin extends Controller { Code: <div id="primary"> EDIT: Turns out I can't attach a patch file. I'll just post it here. One caveat to be aware of. Files have different line endings between computer systems. I work in the UNIX standard LF. Just make sure both the patch and the Loader.php file have the same encoding and line endings or the patch will fail. Save the following code to a file on your computer. Code: 34d33 Hope this helps! Cheers, -- volte |
Welcome Guest, Not a member yet? Register Sign In |