Welcome Guest, Not a member yet? Register   Sign In
help with view within a view
#6

[eluser]tzultacah[/eluser]
The main.php layout view:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
&lt;html&gt;
&lt;?php
    $base_url = $layout['base_url'];
?&gt;
&lt;head&gt;
    &lt;meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/&gt;
    &lt;meta name="description" content="content blah blah"/&gt;
    &lt;meta name="keywords" content="keywords n stuff"/&gt;
    &lt;meta name="author" content="ME!"/&gt;
    &lt;base href="&lt;?php echo $base_url;?&gt;"/&gt;
    &lt;link rel="icon" type="image/x-icon" href="app/img/favicon.ico" /&gt;
    &lt;link rel="stylesheet" type="text/css" href="app/css/grey-black.css" media="screen"/&gt;
    &lt;link rel="stylesheet" type="text/css" href="app/css/grey-black-main.css" media="screen"/&gt;
    &lt;link rel="stylesheet" type="text/css" href="app/css/messages.css" media="screen"/&gt;
&lt;?php
    // now you can specify additional css for ... down to even the function of a controller
    foreach($layout['media']['css'] as $css_file) {
?&gt;
    &lt;link rel="stylesheet" type="text/css" href="&lt;?php echo $css_file; ?&gt;" /&gt;
&lt;?php
    }
    // same here with the javascript. If you have a controller function that needs additional
    // javascript, you can assign it to ONLY that controller function/view
    foreach($layout['media']['js']['scripts'] as $script_name) {
?&gt;
    [removed][removed]
&lt;?php
    }
?&gt;
    [removed][removed]
    &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;

&lt;!-- zero margin = wide layout --&gt;

&lt;body style="margin: 0;"&gt;
&lt;!-- CONTAINTER --&gt;
    <div class="container">
        <div class="header">
&lt;!-- this is actually where I have my header stuff, you could just include a header.php
view and shove all this code in there --&gt;
            &lt;?php echo html_link('<span>'.$layout['header']['title'].'</span>', $layout['header']['link'], array(), false, false); ?&gt;
        </div>
        <div class="stripes"><span></span></div>
&lt;!-- Top menu bar --&gt;
        <div class="nav">
&lt;?php  
    if (checkArray($layout['menu_bar'])) {
        foreach ($layout['menu_bar'] as $title => $url_seg) {
            echo html_link($title, $url_seg, array(), false, false)."\n";
        }
        if (isset($layout['user']) && checkArray($layout['user'])) {
            echo '<div style="text-align:right;padding:7px;">Welcome '.$layout['user']['username'].'</div>';
        }
    } else {
        echo html_link('Home', $base_url, array(), false, false());
    }
?&gt;
            <div class="clearer"><span></span></div>
        </div>
&lt;!-- END Top menu bar --&gt;
        <div class="stripes"><span></span></div>
        <div class="main">
            <div class="left">
&lt;!-- CONTENT --&gt;
                <div class="content">
&lt;?php // flashdata used with redirects with session information.... errors etc ?&gt;
&lt;?php include 'flashdata.php'; ?&gt;
&lt;?php // and here you echo your view file for the controller of the page you are accessing ?&gt;
&lt;?php echo $layout['content']; ?&gt;
                </div>
&lt;!-- END CONTENT --&gt;
            </div>
&lt;!-- RIGHT NAV --&gt;
            <div class="right">
                <div class="subnav">
&lt;?php include('right_nav.php'); ?&gt;
                </div>
            </div>
&lt;!-- END RIGHT NAV --&gt;
            <div class="clearer"><span></span></div>
        </div>
&lt;!-- FOOTER --&gt;
        <div class="footer">
&lt;?php include('footer.php'); ?&gt;
        </div>
&lt;!-- END FOOTER --&gt;
    </div>
&lt;!-- END CONTAINTER --&gt;
&lt;/body&gt;
&lt;/html&gt;

The final step is in your controllers
--snipit--
Code:
class Users extends MY_Controller {

    var $sidebar    = array(
    );
    public function __construct() {
        parent::__construct();
                // this is needed in each controller, basically -- if you have used a different
                // layout somewhere, this sets it back to the main one.
        $this->load->library('layout', 'layouts/main');
Now in my "page" function, I use:
Code:
public function index() {
        $data = array();
        
        $this->layout->view('users/index', $data);
    }

Ok so.... how does all this benefit me?
here we go.
1. Now you have a site layout, with headers, footers, side content ... whatever you want
2. All of these parts and pieces are included without having to tell each page to do so.
3. If (for example) the "display" page of users (function display) needs a different layout, all you have to do is tell that function to use a different layout (which you have to define of course) like so:
Code:
public function display() {
        $this->layout->layout = 'layouts/plain';

*Note: I do believe I have broken some MVC principles here with using raw includes to files in a "view" file, but this solution has been very clean, and we're not calling controller functions or anything fancy here in the included files. All data needed for them is fed to them accordingly. They are view files, and this is a means of setting up a solid layout without trying to set content for "static views" each time you want to use them.


Messages In This Thread
help with view within a view - by El Forum - 03-15-2009, 06:47 PM
help with view within a view - by El Forum - 03-15-2009, 07:35 PM
help with view within a view - by El Forum - 03-20-2009, 02:01 PM
help with view within a view - by El Forum - 03-21-2009, 03:46 AM
help with view within a view - by El Forum - 03-21-2009, 05:08 AM
help with view within a view - by El Forum - 03-21-2009, 05:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB