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

[eluser]jwindhorst[/eluser]
In the example I gave above, you have the ability to send data to both your header templates as well as your view_templates. It may not solve your problem out of the box, but it might get you close. Also, I believe you can access the session vars from the views.
#12

[eluser]rootman[/eluser]
The problem about your approach jwindhorst is imho that you have to call the function every time you want to create output and you got to pass the header/footer data every single time. This is excactly what I want to avoid.

My structure is like this: I have a view and i include the header and the footer inside of the view. This way i have full control over my output inside of my views and the designer can even integrate different headers without touching the controller. I dont want to create complicated constructs to load different views with different data for each one, I wanna keep it simple Smile Just make some variables globally availiable in all views, as if I passed them. Maybe there is a way to pass global variables in advance and just let them "hang" there, no matter if used or not.

Sure i could write a helper that pulls in the desired variables, but I believe that to be over the top, shouldnt it be simple just to tell the output class to have some more vars availiable? I dont want the designer to notice that there is something even going on different from the normal approach ... Wink
#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!




Theme © iAndrew 2016 - Forum software by © MyBB