CodeIgniter Forums

Full Version: [SOLVED] set_theme() not working in Template Class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Devon Lambert[/eluser]
Hi all,

Checked all around and couldn't find anyone that had this problem or something similar so here goes.

I am making use of Phil Sturgeon's Template library. I have set up 3 extended controllers similar to what Phil has done in PyroCMS. There's the MY_Controller, which is then extended in an Admin_Controller and a Public_Controller.

I am trying to set_theme in my Admin_Controller and then using that setup my template within an admin controller. However, my CI app keeps complaining that it can't find my "partials" folder.

The Partials folder is located in my themes directory, under the theme name, i.e.:

./App/
./Other APP Stuff/
./Themes/
./theme_name/
./views/
./partials/
./login_header.php
./login_footer.php
./footer.php
./header.php

The app only seems to like it when I move the partials folder to the Application's Views folder? Checking PyroCMS it looks like Phil never made use of set_theme when he was making calls to partials, so I'm at a loss.

Anyone else run into this issue? Or perhaps Phil is around and can lend the assist? :-)

Thanks all.

El Forum

[eluser]Phil Sturgeon[/eluser]
Yeah I didn't bother adding theme partials... should probably do that. Tongue

El Forum

[eluser]Phil Sturgeon[/eluser]
Have a play with _load_view() and see if you can get a nice solution for it. I have other stuff to work on today. :-(

Code:
private function _load_view($view = '', $search = TRUE, $parse_view = TRUE)
    {
        // Hunt it down like a dog, through themes and modules
        if($search == TRUE)
        {
            $theme_view = 'themes/' . $this->_theme . '/views/modules/' . $this->_module . '/' . $view;
            
            if( $this->_theme && file_exists( APPPATH . $theme_view . EXT ))
            {
                $this->CI->load->library('parser');
                return $this->CI->parser->parse('../'.$theme_view, $this->data, TRUE);
            }

            // Nope, just use whatever's in the module
            else
            {
                if($this->_parser_enabled === TRUE && $parse_view === TRUE)
                {
                    $this->CI->load->library('parser');
                    return $this->CI->parser->parse( $this->_module.'/'.$view, $this->data, TRUE );
                }
                
                else
                {
                    return $this->CI->load->view( $this->_module.'/'.$view, $this->data, TRUE );
                }
            }
        }
        
        // Load exactly what we asked for, no f**king around!
        else
        {
            if($this->_parser_enabled === TRUE && $parse_view === TRUE)
            {
                $this->CI->load->library('parser');
                return $this->CI->parser->parse( $view, $this->data, TRUE );
            }
            
            else
            {
                return $this->CI->load->view( $view, $this->data, TRUE );
            }
        }
    }

El Forum

[eluser]Devon Lambert[/eluser]
Here's how I fixed it all Phil. I noticed you made an assumption when building this library, and that is that everyone would want any view served up within a theme to be parsed through the parser library. I can see how this would normally make perfect sense but in my case, I will be the only one making changes to my templates, so I'll opt for the small speed boost from not using the parser class, thank you very much. :-)

Since I don't plan on ever using the parser library (nor do I assume that a view within a theme requires a parser)

I change the following at line 48:

Code:
private $_parser_enabled = TRUE;

to:

Code:
private $_parser_enabled = FALSE;

I also change the following at line 161:
Code:
if($this->_parser_enabled === TRUE || $this->_theme)

to:

Code:
if($this->_parser_enabled === TRUE)

Finally, I replaced the following bit of code at line 346:

Code:
$this->CI->load->library('parser');
return $this->CI->parser->parse('../'.$theme_view, $this->data, TRUE);

to:

Code:
if($this->_parser_enabled === TRUE) {
    $this->CI->load->library('parser');
    return $this->CI->parser->parse('../'.$theme_view, $this->data, TRUE);
} else {
    return $this->CI->load->view('../'.$theme_view, $this->data, TRUE);
}
Now on to the fix

At line 110:

I changed:

Code:
$template['partials'][$name] = $this->_load_view( $partial['view'] , $partial['search']);

to:

Code:
$template['partials'][$name] = $this->_load_view( $partial['view'] , $partial['search'], TRUE);
then at line 337:

change:

Code:
private function _load_view($view = '', $search = TRUE)

to:

Code:
private function _load_view($view = '', $search = TRUE, $partial = FALSE)
Finally, I needed to modify the $theme_view search function

Replace line 342:

Code:
$theme_view = 'themes/' . $this->_theme . '/views/modules/' . $this->_module . '/' . $view;

with:

Code:
// Do we have a partial view within a module within a theme?
if ($this->_theme && $this->_module && $partial) {
    $theme_view = 'themes/' . $this->_theme . '/views/modules/' . $this->_module . '/partials/' . $view;
} else if ($this->_theme && $partial) { // Do we have a partial view within a theme?
    $theme_view = 'themes/' . $this->_theme . '/views/partials/' . $view;
} else if ($this->_theme && $this->_module) { // Do we have a view within a module within theme?
    $theme_view = 'themes/' . $this->_theme . '/views/modules/' . $this->_module . '/' . $view;
} else if ($this->_theme ) { // Do we have a view within a theme?
    $theme_view = 'themes/' . $this->_theme . '/views/' . $view;
}



There may be a better way to accomplish the above, but this all works for me, for now. :-)