开发者

Codeigniter : variables scope when calling a view from within a view. Odd

开发者 https://www.devze.com 2023-01-21 08:25 出处:网络
I am confused about variables scope when calling a view from within a view. I tested a bit and found: If the variables are originally passed from a controller, there is no need t开发者_如何学JAVAo

I am confused about variables scope when calling a view from within a view. I tested a bit and found:

  1. If the variables are originally passed from a controller, there is no need t开发者_如何学JAVAo do something to pass the variables from a view to a view.
  2. If the variables are declared in a view, I have to explicitly pass the variables from a view to a view. (e.g. : $this->load->view("hoge", $data);)

I find it bit odd about the second case because my understanding was $this->load->view() is codeigniter version of the php include() which doesn't require me to pass variables explicitly.

Can anyone guess/shed a little light on why they did this?


If you look at the Loader library's _ci_load method (which view() calls), around line 639 in the latest version, you'll see this bit of code:

/*
 * Extract and cache variables
 *
 * You can either set variables using the dedicated $this->load_vars()
 * function or via the second parameter of this function. We'll merge
 * the two types and cache them so that views that are embedded within
 * other views can have access to these variables.
 */ 
if (is_array($_ci_vars))
{
    $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
}
extract($this->_ci_cached_vars);

That's why your variables passed to the view are available automatically in nested views.

But your locally declared variable aren't.. because they aren't passed on to the next view() call.

Note that it merges the variables, so technically, you could just pass to your subview the variable that are changed in the top view, the rest will be inherited automatically.

IMHO though, I think that for the sake of clarity and other potential people reading your code it's best to always pass on explicitly all the variables that your subview will require.. code becomes easier to read/debug.

Note: a side effect of the caching is that if you have 2 subviews, variables passed to the first one will get cached and get automatically passed on to the second view as well.. that can lead to debugging trouble sometimes.

0

精彩评论

暂无评论...
验证码 换一张
取 消