Welcome Guest, Not a member yet? Register   Sign In
Multiple views sharing variables "upwards"

Sorry for the strange subject...

I'm using a pretty basic, home-brewed template system. Basically, my controller methods call a "render" function that calls a layout view, then the layout view then calls the "content view". I'd like to know if there's a way for a variable set in the content view to be used by the layout view. Perhaps it's more clear with examples:

A method from a controller:
function about()
    $data['page_title'] = 'About Our Wonderful Stuff';
    $data['extra_css'][] = 'about';
    $this->view->render($data, 'about');
Note: In the last line, 'about' is the name of the view to load.

The render function (in a custom library, view.php):
function render($data, $view, $layout='main')
    $data['view'] = $view;
    $data['CI'] =& $this->CI; // Passed through so that the layout file can call "load->view" in order to load the view.
    $data['data'] = $data; // Add $data to itself so that $data will be available in the view (after load->view is called a second time)
    $this->CI->load->view("layouts/{$layout}", $data);
Note: I'm adding $view to the $data array.

The "main" layout (as opposed to the "popup" layout or any other layout that the site may use):
    <title><?= SITE_NAME ?> : <?= $page_title ?></title>
    <link type="text/css" rel="stylesheet" href="/css/reset.css" />
    <link type="text/css" rel="stylesheet" href="/css/main.css" />
    <? if(isset($extra_css)): ?>
    <? foreach($extra_css as $css) :?><link type="text/css" rel="stylesheet" href="/css/<?= $css ?>.css" /><? endforeach; ?>
    <? endif; ?>


    <div id="main_header" class="clearfix">
        Site header goes here

    <div id="main_content" class="clearfix">
        &lt;?= $CI->load->view($view, $data); ?&gt;

    <div id="main_footer" class="clearfix">
        Site footer goes here

Two things to note: The page_title and extra_css are variables. The "main_content" div is being "loaded" as it's a separate view.

And the "about" view:
<div id="toffees">
    Blah, blah blah...

So... my main question: How can I move page_title and extra_css from the "about" method in the controller into the "about" view? So that my "about" view would look like this:
&lt;? $data['page_title'] = 'About Our Wonderful Stuff'; ?&gt;
&lt;? $data['extra_css'][] = 'about'; ?&gt;

<div id="toffees">
    Blah, blah blah...

Is it possible?


Messages In This Thread
Multiple views sharing variables "upwards" - by El Forum - 12-03-2009, 10:32 AM
Multiple views sharing variables "upwards" - by El Forum - 12-03-2009, 06:45 PM
Multiple views sharing variables "upwards" - by El Forum - 12-03-2009, 07:21 PM
Multiple views sharing variables "upwards" - by El Forum - 12-04-2009, 01:05 AM

Theme © iAndrew 2016 - Forum software by © MyBB