Welcome Guest, Not a member yet? Register   Sign In
Header and footer
#10

[eluser]IanJ[/eluser]
After playing with CI for a couple of hours, I came up with this little solution I added to the loader.
Code:
/*------------------------------------------------------------
    |
    | viewLayout($template, $data = array(), $layoutData = array(), $layout = 'default')
    |
    | Renders output with a layout, rather than using
    | two statements in the code, this will do the same
    | in one. Layouts are stored in the views/layouts
    | directory.
    |
    | Required:
    | $template -     the tempalte to render as normally would
    |                with $this->load->view('template');
    |
    | Optional:
    | $data -        the data to be rendered with the template
    |
    | $layoutData -    data to be included in the layout, DO NOT
    |                USE content_for_layout in the $layoutData
    |                array
    |
    | $template -     the template to render, defaults to default
    |
    +------------------------------------------------------------*/
    function layout($template, $data = array(), $layoutData = array(), $layout = 'default'){
        
        $layoutData['content_for_layout'] = $this->_ci_load(array('view' => $template, 'vars' => $this->_ci_object_to_array($data), 'return' => true));

        $this->_ci_load(array('view' => "layouts/$layout", 'vars' => $this->_ci_object_to_array($layoutData), 'return' => false));
        
    }
It treats the layout as just another view so you can pass a normal array of variables in and parse them in the layout just like a normal view. Just make sure to add
Code:
<?php echo $content_for_layout; ?>
to your layout files.

Ian


Messages In This Thread
Header and footer - by El Forum - 07-13-2007, 01:33 PM
Header and footer - by El Forum - 07-17-2007, 05:32 PM
Header and footer - by El Forum - 07-18-2007, 05:44 AM
Header and footer - by El Forum - 07-18-2007, 12:42 PM
Header and footer - by El Forum - 07-19-2007, 09:14 PM
Header and footer - by El Forum - 07-20-2007, 07:15 AM
Header and footer - by El Forum - 07-20-2007, 07:38 AM
Header and footer - by El Forum - 07-20-2007, 09:10 AM
Header and footer - by El Forum - 07-23-2007, 06:11 AM
Header and footer - by El Forum - 07-27-2007, 03:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB