rudimentary template page chunks in my views, comments welcome |
[eluser]Flying Fish[/eluser]
I'm at the point when I need to start including common chunks of html in my views, like a header, footer, etc. My goals were be able to clearly include common page chunks in my view files make changes in one master template file avoid using something complex from the wiki that I can't understand :-) Here's my possibly laughable attempt, I created a custom library called 'Template' and put it in Code: /system/application/libraries/Template.php Here's what the file looks like Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); your thoughts and comments are welcome [/code]
[eluser]Aea[/eluser]
You don't need a library / class to do this but there's nothing wrong with that. 1) Move your $CI instance to your constructor, that way that reference is retrieved once and stored, instead of retrieved every time you want a piece. Code: $this->CI =&get;_instance(); Of course you could just comment that line out, it's not necessary since you're not accessing anything that needs it ![]() 2) Don't iterate to get values, there's a simpler way: Code: return $pieces[$piece];
[eluser]jedd[/eluser]
I like it .. but I can't work out why. Can you just clarify the intent here. Instead of typing this (to get a view component) in my controller: Code: $header_viewette = $this->load->view ('_inc_header', $data, TRUE); I'd instead type this: Code: $header_viewette = $this->Template->show ('header'); Oh, and I'd load the Template library for/in every Controller (presumably as part of the autoload).
[eluser]Flying Fish[/eluser]
@Aea thanks for the input on return the array $pieces[$piece], that's great! but I'm having trouble moving the $CI to the constructor, I get the following error Quote:A PHP Error was encountered I thought I needed in the show function so I need to do $ci->load->view instead of $this->load->view, so I don't create a duplicate of the master code igniter object @jedd yes basically In my view file I will just have these little snippets around my other CI code [code] <?=$this->template->show('head')?> [code] at least for me, it's easy to read, and takes up very little space in the code
[eluser]Aea[/eluser]
Make sure that it's $this->CI =& get_instance(); And use it as $this->CI->load->view(...) Let me know if this fixes it. Also: Shove your $pieces in as a class scope variable, so same $this->pieces = ... deal. No reason to regenerate this array every time, especially since loading a view isn't trivial ![]()
[eluser]Flying Fish[/eluser]
@Aea newbie question...could you give me a quickie example of how it would look as a class scope variable I'm pretty to green at php development I was following the user guide example of creating a custom library, and I didn't see any code examples of $this->CI =& get_instance(); http://ellislab.com/codeigniter/user-gui...aries.html how is that different than $CI =& get_instance();
[eluser]Flying Fish[/eluser]
using this code with the $this->$CI... Code: class Template I get this error Quote:A PHP Error was encountered
[eluser]Aea[/eluser]
$CI =& get_instance(); in a method only allows that variable a method scope, when you call that method again it doesn't exist and is regenerated. When you do $this->CI you give the class that variable, so it persists between method calls and even between methods. Of course you could do .. class Blah { $CI &=... } Which is the equivalent of doing $this->CI in the constructor.
[eluser]Aea[/eluser]
Once again, don't call it $CI if you're making it a class variable, it has to have a $this-> to have a class scope.
[eluser]Flying Fish[/eluser]
dude, I'm sorry I really appreciate you being so helpful, but I can't seem to wrap my head around it do you think you could show me an example of where the vars should go and what they should look like? [code] class Template { function __contstruct() { } function show() { } } [code] |
Welcome Guest, Not a member yet? Register Sign In |