2

Let's say you load a view from a controller and that view loads another view that uses a lot of the same variables as the view that loaded it. How do get both views to share those variables? Thanks

5
  • @Vickel. Did you try to load view in another view? Commented Jan 18, 2014 at 17:26
  • @Vickel. No you didn't try. I have done in my projects to avoid redundancy, we make a single file and loaded in another view file too. Commented Jan 18, 2014 at 17:29
  • @kumar_v now I'm learning... can you please post an example? Commented Jan 18, 2014 at 17:31
  • @Vickel stackoverflow.com/questions/9402924/… Commented Jan 18, 2014 at 17:34
  • 2
    I've just tried it out, you're right, it works! thanks for letting me know something new, very helpful, indeed Commented Jan 18, 2014 at 17:36

3 Answers 3

3

All variables you define to a view, are passed down to views loaded within the parent one. You don't need to pass them down an other level through the second array parameter, unless you want to override a specific value.

Basically, define all variable in the 2nd parameter to the "parent" view and both views will have these variables.

Sign up to request clarification or add additional context in comments.

Comments

1

For ex: you are loading view in controller:

$data["msg"] = "hi";
$this->load->view("view_file",$data);

In view_file, you are loading another view file

$this->load->view("view_file2",array("msg"=>$msg)); // here msg is extracted from first view file

3 Comments

Now way to just "relay" the variables?
I mean relay the whole set of variables without having to specify them one by one.
Yes you can do. It's all your logic. You can pass it as array.
1

In one view, i set this:

window.variable= variableToAnotherView;

windows.variable is to pass the variable globaly, so you will be able to call it in another view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.