Partial Views with Local Variable Scope In CodeIgniter
I have something like the following code in one of my views:
<?php $this->load->view('_validation_error', array('errors' => $errors, 'field' => 'address_1', 'is_required' => true)); ?>
... some html ...
<?php $this->load->view('_validation_error', array('errors' => $errors, 'field' => 'address_2')); ?>
This allows me to use $errors
, $field
and 开发者_运维百科$is_required
in the partial. This is good, although what's happening here is that if I don't supply the 'is_required' value on subsequent calls to the partial, it's getting set to what the last value was (true in this case).
I could just put the value in every time but I was hoping to have it behave like an optional parameter to a function. Is there any functionality in CodeIgniter that allows partials to have their own local variable scope.
Sweet! Never mind, I think I figured out a hack to get around it.
instead of going:
<?php $this->load->view('_validation_error', array('errors' => $errors, 'field' => 'address_1', 'is_required' => true)); ?>
I can go:
<?php $this->load->view('_validation_error', array('data' => array('errors' => $errors, 'field' => 'address_1', 'is_required' => true))); ?>
That way I have access to $data['errors']
, $data['field']
and $data['is_required']
.
That way any values not passed in are overwritten and by default are optional.
:)
精彩评论