Welcome Guest, Not a member yet? Register   Sign In
code duplication advice
#13

[eluser]jwindhorst[/eluser]
I realize this thread has gotten pretty old, but I rewrote the above function as an extension to the loader class, and thought it might be useful, so here it is:

Code:
class MY_Loader extends CI_Loader {

    /**
     * attempting to overload this function to include our header and footer
     **/
    function view($views, $vars = array(), $return = FALSE)
    {

        // pass any header vars on to the header    
        $header_vars = (isset($vars['header_data'])) ? $vars['header_data'] : array();

        // pass any footer vars on to the header    
        $footer_vars = (isset($vars['header_data'])) ? $vars['header_data'] : array();

        $doc = $this->_ci_load(array('_ci_view' => 'layout/header', '_ci_vars' => $this->_ci_object_to_array($header_vars), '_ci_return' => $return));

        // if it's an array we need to loop through them
        if(is_array($views))
            foreach($views as $view)        
                $doc .= $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        else
            $doc .= $this->_ci_load(array('_ci_view' => $views, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));

        $doc .= $this->_ci_load(array('_ci_view' => 'layout/footer', '_ci_vars' => $this->_ci_object_to_array($footer_vars), '_ci_return' => $return));
        return $doc;
    }

}

Basic usage is that anything if you set an array inside your views data array called header_data, it will get passed to the header, same with footer_data getting passed to the footer.

I haven't found a use for footer_data yet, but header_data I usually pass a js_scripts array that sources in all of my javascripts. This way they are only included on the pages that use them, and they still get to be sourced in the head tag instead of hacked into the body somewhere.

Cheers!


Messages In This Thread
code duplication advice - by El Forum - 03-24-2009, 08:39 PM
code duplication advice - by El Forum - 03-24-2009, 08:53 PM
code duplication advice - by El Forum - 03-24-2009, 09:24 PM
code duplication advice - by El Forum - 03-26-2009, 03:07 AM
code duplication advice - by El Forum - 03-26-2009, 05:11 AM
code duplication advice - by El Forum - 03-26-2009, 12:26 PM
code duplication advice - by El Forum - 03-26-2009, 06:37 PM
code duplication advice - by El Forum - 09-30-2009, 02:31 PM
code duplication advice - by El Forum - 09-30-2009, 02:44 PM
code duplication advice - by El Forum - 09-30-2009, 02:49 PM
code duplication advice - by El Forum - 09-30-2009, 02:59 PM
code duplication advice - by El Forum - 09-30-2009, 03:34 PM
code duplication advice - by El Forum - 01-10-2010, 06:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB