Welcome Guest, Not a member yet? Register   Sign In
rudimentary template page chunks in my views, comments welcome
#1

[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');

/*
    The Template Class provides simple templating functionality
    
    You will see this code at the beginning of each function
    
    $CI =& get_instance();
    
    It assigns a variable to this instance of the codeigniter object.
    This allows me to use native codeigniter classes like session userdata in this custom class

*/

class Template
{
    
    function __construct()
    {
    
    }
    
    function show($piece)
    {        
        $CI =& get_instance();
        
        $pieces = array(
            'html_head'   => $CI->load->view('_inc/head', '', TRUE),
            'masthead'    => $CI->load->view('_inc/masthead', '', TRUE)
        );
        
        foreach ($pieces as $key => $value):
            
            if($piece === $key)
            {
                return $value;
            }
        
        endforeach;

    }
    
}


/*
    End of file Template.php
    Location: /system/application/libraries/Template.php
*/

I can auto load this library, and when I need a page chunk I just call something like this in my view
[code]<?=$this->template->show('head')?>


your thoughts and comments are welcome


[/code]
#2

[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 Smile

2) Don't iterate to get values, there's a simpler way:

Code:
return $pieces[$piece];
#3

[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).
#4

[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

Severity: Notice

Message: Undefined variable: CI

Filename: libraries/Template.php

Line Number: 27
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/Template.php

Line Number: 27

Fatal error: Call to a member function view() on a non-object in /Applications/MAMP/htdocs/staging2/system/application/libraries/Template.php on line 27

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
#5

[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 Smile
#6

[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();
#7

[eluser]Flying Fish[/eluser]
using this code with the $this->$CI...

Code:
class Template
{
    
    function __construct()
    {
        $this->$CI =& get_instance();
    }
    
    function show($piece)
    {        
        
        $pieces = array(
            'head'   => $CI->load->view('_inc/head', '', TRUE),
            'masthead'    => $CI->load->view('_inc/masthead', '', TRUE)
        );
        
        return $pieces[$piece];
    }
    
}


I get this error
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: CI

Filename: libraries/Template.php

Line Number: 20

Fatal error: Cannot access empty property in /Applications/MAMP/htdocs/staging2/system/application/libraries/Template.php on line 20
#8

[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.
#9

[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.
#10

[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]




Theme © iAndrew 2016 - Forum software by © MyBB