Welcome Guest, Not a member yet? Register   Sign In
Variable data in static views
#1

[eluser]Unknown[/eluser]
Hello everyone!

I know it sounds kind of contradictory but that's my problem (and maybe i can catch your attention easily Smile ).

I've started using CI a few days and i'm stuck in a view/controller issue. I've made a layout view that contains the main elements of my system, something like this (just as an example):

In my main layout (let's say layout/layout.php):
...
Code:
<div id=header>
    &lt;?php echo $this->load->view("layout/my_header_view")?&gt;
</div>

<div id=right_column>
    &lt;?php echo $this->load->view("layout/my_right_column_view")?&gt;
</div>

<div id=main>
    &lt;?php echo $this->load->view($may_variate)?&gt;
</div>


And let's assume that i have some variables that the view expects from the controller

Code:
my_header_view => $my_header_view_DATA
my_right_column_view=>$my_right_column_view_DATA


And in my controller, for example the "welcome controller"
...
Code:
function index(){

     $data["my_header_view_DATA"] = "Tralala"
     $data["may_variate"] = "Hello there, i'am the main section!"
     $data["my_right_column_view_DATA"] = "--&gt;Something that i'm bringing from my database<--"
     $this->load->view('layout/layout', $data);

}


My question is. It's abssolutly necessary to load in every controller the '$data["my_right_column_view_DATA"]' variable?, (What if i have hundreds of controllers? it's not really scalable or maintainable)

Maybe one workaround is to let the "my_right_column_view" have a call to my model and load the data from the database, but i don't think that's a good practice.

Is there something that i'm missing? It's possible to have a "data preload" for the right column view? (maybe a trick with hooks?)

Thanks a lot, i'm kind of disoriented here :down:

Ps. Sorry for my english, i've lost a lot of practice :red:
#2

[eluser]nelson.wells[/eluser]
You could write a custom controller that inherits from the base controller, load the variables in that controller's constructor, and then have all of your other controllers inherit from that constructor. Something like this (written in the browser, so untested.):

Code:
class MY_Controller extends CI_Controller
{
     function __construct()
     {
         //global variables
         $data["my_header_view_DATA"] = "Tralala";
         $data["my_right_column_view_DATA"] = "--&gt;Something that i'm bringing from my database<--";    

         $this->load->vars($data);
     }
}

Code:
class Welcome extends MY_Controller
    {
         //your normal controller methods
    }

Any controller that extends MY_Controller will have access to $my_header_view_DATA in the view it loads. Like I said, it is untested so it may have typos, but the idea is there. Hope that helps.
#3

[eluser]Unknown[/eluser]
I already had a custom controller so it was pretty easy to test and worked like charm!

Thanks a lot!
#4

[eluser]elambiguo[/eluser]
Basically....

Code:
<div id=header>
    &lt;?php echo $this->load->view($header_data)?&gt;
</div>

<div id=right_column>
    &lt;?php echo $this->load->view($right_column_data)?&gt;
</div>

<div id=main>
    &lt;?php echo $this->load->view($may_variate)?&gt;
</div>


I thing that this is correct....

In cotroller......
Code:
function index(){

     $data["header_data"] = "Tralala" // view for header...
     $data["may_variate"] = "Hello" // View for ......
     $data["right_column_data"] = "Something" // view for right column....
     $this->load->view('layout/layout', $data);

}


Quote:My question is. It's abssolutly necessary to load in every controller the '$data["my_right_column_view_DATA"]' variable?, (What if i have hundreds of controllers? it's not really scalable or maintainable)

Not... you can use one variable session for all....
Only change the variable in the load->view for one $this->userdata

Quote:Maybe one workaround is to let the "my_right_column_view" have a call to my model and load the data from the database, but i don't think that's a good practice.

Is there something that i'm missing? It's possible to have a "data preload" for the right column view? (maybe a trick with hooks?)

You can see the user guide... or the wiki...

Quote:Ps. Sorry for my english, i've lost a lot of practice :red:

Uh.... one more!!! :-)

I hope you help....




Theme © iAndrew 2016 - Forum software by © MyBB