CodeIgniter: Accessing view variables from helper
I am extending form_helper that will populate data from an array in view.
E.g:
//Controller - user_controller.php
User_Controller extends CI_Controller{
function edit(){
$data['record'] = array('username'=>'robert','email'=>'simplerobert@google.com');
$this->load->view('edit',$data);
}
}
//View - edit.php
<?= $record['username']; ?> >> 'robert'开发者_Python百科
<?= simple_input('halo'); ?>
//Helper - MY_form_helper.php
function simple_input($name){
var_dump($record); >> Undefined variable: record
return "<input type='text'/>";
}
I thought helper should load up the variables from view. Didn't really understand how it works. How can I access the view variables from helper?
Try passing the variable in the function:
//...
//View - edit.php
<?= $record['username']; ?> >> 'robert'
<?= simple_input('halo', $record); ?>
//Helper - MY_form_helper.php
function simple_input($name, $record){
var_dump($record);
return "<input type='text'/>";
}
helper is function, so you need to pass the var into the function to use it.
(tttony wrote about this.)
I think you'd better make another view.
in that case you don't need to pass the vars.
//View - edit.php
<?= $record['username']; ?> >> 'robert'
<?= $this->load->view('simple_input'); ?>
//View simple_input.php
var_dump($record);
echo "<input type='text'/>";
helper is function, so you need to pass the var into the function to use it. (tttony wrote about this.) I think you'd better make another view. in that case you don't need to pass the vars.
//View - edit.php
<?= $record['username']; ?> >> 'robert'
<?= $this->load->view('simple_input'); ?>
//View simple_input.php
var_dump($record);
echo "<input type='text'/>";
精彩评论