我对每个页面都有一个模板设计,它没有任何问题,现在我有一个表单验证和模板的问题。
my_controller //inside my controller
function __construct()
{
parent::__construct();
$this->load->library('template');
}
public function page1(){
$this->template->write('title', 'COMPANY');
$this->template->write_view('content', 'company');
$this->template->render();
}
public function validation(){
//for page1 form validation
//form validation here
if ($this->form_validation->run() == FALSE){
// here i need to call my page1 again to update the form error
// option1: $this->load->view('page1'); // this is just loading page1, not loading with my template, template file has header, footer, js and css file includes
// option2: $this->template->load('template', 'page1'); // it is loading page withing page
// option3: $this->template->write_view('content', 'page1');
// $this->template->render(); // this is same as option2
} else {
$this->load->view('formsuccess');
}
}
所以我的问题是我如何能调用相同的视图(在一个控制器内)多次,当它已经加载了模板?
编辑更清晰
我的模板文件只是一个文件(不像头,页脚分开)将有所有的头信息和页脚信息与共同的样式表和脚本文件。我有
<?php $content ?>
变量名,以便从控制器传递我的内容。
的例子见my page1 function(),
$this->template->write('title', 'COMPANY'); // to write a value on variable
$this->template->write_view('content', 'page1'); //view page to write on template
$this->template->render(); // final template render
此页有表单验证,如果我的表单验证是FALSE,我怎么能重新加载验证错误的模板页面?
我已经尝试了很多方法,请检查我的选项1,2,3,我从来没有得到一个解决方案来解决这个问题,现在请告诉我如何才能解决这个问题?
如果我理解正确,您想要显示验证错误吗?
在View文件中放入以下内容:
if语句短格式:
<?php if ( validation_errors() ) :?>
<div>
<?php echo $validation_errors();?>
</div>
<?php endif;?>
正则if语句格式:
<?php if ( validation_errors() ) {?>
<div>
<?php echo $validation_errors();?>
</div>
<?php }?>
验证错误Doc