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

[eluser]janroald[/eluser]
Can I turn off the annoying "magic" occuring when passing to the view? If I create me an array $colors, i don't want it broken up into $red, $green etc. Please don't tell me this is a default feature i MUST accept... Let me keep my arrays and objects just the way they are.
#2

[eluser]ironlung[/eluser]
I think you can pass the array in an array and you will be fine

ie $colours = array('red','green','blue');

$data = array('colours'=>$colours);

add any other variables you like to $data and pass it to the view and the colours array will be dandy
#3

[eluser]janroald[/eluser]
Yes, I'm aware of that, but thats really just an annoyance. I create my variables with the intent to use them. Having to create another level of array hierarchy just for show seems utter idiotic. In 99% of the situations i prefer using array variables for best order in my applications. In the situation i want my array to be translated into singular variables, I would use the built-in php function extract().
#4

[eluser]ironlung[/eluser]
ok kewl Smile
#5

[eluser]jedd[/eluser]
[quote author="janroald" date="1239465385"]Yes, I'm aware of that, but thats really just an annoyance.[/quote]

Perhaps you find it annoying because you are unfamiliar with it, and do not understand the intent and benefits of this approach.

Quote: I create my variables with the intent to use them. Having to create another level of array hierarchy just for show seems utter idiotic.

If you were designing your own MVC framework, how would you pass multiple variables (of different types) to a view? Keeping in mind the design goal of having an elegant passing mechanism (such as CI's single variable approach).
#6

[eluser]Mike Ryan[/eluser]
Seconding jedd's post... if you only ever pass a single simple data type to a view I can see how this would be inconvenient. However, I think the flexibility added by this approach is worth such a trivial annoyance.

If it really bugs you, open system/libraries/loader.php and change line 307 to:
Code:
return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $vars, '_ci_return' => $return)); //untested

ironlung's example could be even shorter:

Code:
$data['colors'] = array('red','green','blue');
//... which isn't much longer than:
$colors = array('red','green','blue');
#7

[eluser]janroald[/eluser]
I DO see the intent, however the benefits I'm not so sure about.

Quote:If you were designing your own MVC framework, how would you pass multiple variables (of different types) to a view? Keeping in mind the design goal of having an elegant passing mechanism (such as CI’s single variable approach).

First of all I would not "interfere" with the variables in the passing by default. An approach like CI's I would create as an option. Variables, by any type, passed to a view should be prepared already, so that one can achieive a good separation between business logic and presentational logic. I expect my variables to be actually PASSED, not PROCESSED.

To make myself clearer - I'm just looking for a way to turn off the magic. My variables are already prepared.




Theme © iAndrew 2016 - Forum software by © MyBB