Welcome Guest, Not a member yet? Register   Sign In
passing variables to views
#1

[eluser]mironcaius[/eluser]
Hello,
I have just started working with CI but I am having problems with views.
I want to be able to pass variables and objects to a main view, that will include different files.

Something like:
Code:
$this->load->view('page', 'page1');
//this is how i do it in another CMS
$this->view->assign('page', $page_data);
$this->view->assign('body', 'help');
this would show like:
Code:
<html>
<body>
<?php include $page;?>
</body>
</htm>

The idea behind this to pass variables and control what blocks or php pages to include.
Also is their a method to pass variables to the view without loading a php page?
Please help because I am thinking to drop CI because of this lack.
#2

[eluser]Dennis Rasmussen[/eluser]
[quote author="mironcaius" date="1290002738"]Please help because I am thinking to drop CI because of this lack.[/quote]

Did you even read the documentation?
http://ellislab.com/codeigniter/user-gui...views.html

Yes I'm being harsh on you, because you didn't RTFM and then blame CI.
#3

[eluser]mironcaius[/eluser]
Yes, of course i have read the manual. I have read everything searching for an example. also reviewed some template system.
Anyhow:
Code:
$data['page_title'] = 'Your title';
      $this->load->view('header');
      $this->load->view('menu');
      $this->load->view('content', $data);
      $this->load->view('footer');
this code will load one php file from the views folder one after another. But lets say i simply want to pass a $Authentification object to all of these views.

is their a method to do just that ?
Don't get me wrong, I reaaaallly want to use CI, this is why i am asking for help. Sorry if i said something wrong.
#4

[eluser]Dennis Rasmussen[/eluser]
You can send any kind of data to your view.
If you need some data for all of your views and subviews (you can use $this->load->view('') within another view), you can use $this->load->vars($data) and $data will be available in your entire application (both controllers and views).

You can also access CI from your view with $this->library_name, $this->model etc.
#5

[eluser]mironcaius[/eluser]
Exactly what i was looking for, thanks.

Quote:You can also access CI from your view with $this->library_name, $this->model etc.
This is very nice feature. I created a model.
Code:
&lt;?php
class Model_name extends Model {

    function Model_name()
    {
        parent::Model();
    }

    function testX(){
        echo 'call method directly from model';
    }
}
?&gt;
and from the view i could access the object directly

Code:
&lt;?php
        echo $this->Model_name->testX();

        ?&gt;

From here i simply need to see how to organize the includes or views to display the entire html. Probably i will research and if i cannot find a valid solution i will open a new post, thanks.
#6

[eluser]Dennis Rasmussen[/eluser]
You're welcome Smile




Theme © iAndrew 2016 - Forum software by © MyBB