CodeIgniter Forums
Multiple views sharing variables "upwards" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Multiple views sharing variables "upwards" (/thread-25179.html)



Multiple views sharing variables "upwards" - El Forum - 12-03-2009

[eluser]GregX999[/eluser]
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:
Code:
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):
Code:
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):
Code:
<html>
<head>
    <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; ?>
</head>

<body>

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

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

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

&lt;/body&gt;
&lt;/html&gt;
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:
Code:
<div id="toffees">
    Blah, blah blah...
</div>

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:
Code:
&lt;? $data['page_title'] = 'About Our Wonderful Stuff'; ?&gt;
&lt;? $data['extra_css'][] = 'about'; ?&gt;

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

Is it possible?

Greg


Multiple views sharing variables "upwards" - El Forum - 12-03-2009

[eluser]Isern Palaus[/eluser]
[quote author="GregX999" date="1259879572"]
$data['CI'] =& $this->CI; // Passed through so that the layout file can call "load->view" in order to load the view.[/quote]

You can use $this->load->view() like if you're inside the controller. Check it!


Multiple views sharing variables "upwards" - El Forum - 12-03-2009

[eluser]GregX999[/eluser]
[quote author="Isern Palaus" date="1259909143"]You can use $this->load->view() like if you're inside the controller. Check it![/quote]

Oh, ok, that worked. So I don't need to use that $CI variable.

But, that still doesn't allow variables to be passed "backwards" does it?


Multiple views sharing variables "upwards" - El Forum - 12-04-2009

[eluser]raitucarp[/eluser]
I have solution, maybe you want to use my library

http://raitzine.com/codeigniter-theme-library.html