在codeigniter中创建表单时,我不想在视图中使用很多php-stuff。
控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
//Create login and register-forms
$this->load->helper('form');
$loginform_attributes = array('id' => 'login-form');
$data['loginform_attributes'] = $loginform_attributes;
$this->load->view('home', $data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
视图......
echo form_open('login/register', $loginform_attributes);
有没有办法在控制器中管理整个表单,然后在视图中打印出$form
?当您尝试{{}}时,它似乎没有返回任何内容{1}};在控制器中。
答案 0 :(得分:1)
我建议使用部分视图并将内容作为变量传递给Controller中的主视图,如下所示:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller
{
public function index()
{
//Create login and register-forms
$this->load->helper('form');
$partial['loginform_attributes'] = array('id' => 'login-form');
//Load the login form and assign the output to a variable
$data['form'] = $this->load->view('partial/forms/login', $partial, TRUE);
$this->load->view('home', $data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
来自CI Doc:
$this->load->view('file_name', $data, true/false)
第三个可选参数允许您更改的行为 函数,以便它将数据作为字符串返回而不是发送到 你的浏览器。如果要处理数据,这可能很有用 某种方式。如果将参数设置为true(布尔值),它将返回 数据
然后在home
视图中,只需回显$form
。
echo $form;