CodeIgniter - 控制器 - 用模板加载视图

时间:2014-06-26 13:51:58

标签: php codeigniter

我为每个页面设计了一个模板,它没有任何问题,现在我遇到了表单验证和模板的问题。

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 ?>

模板中的变量名称,用于从控制器传递我的内容。

例如,请参阅我的page1函数(),

   $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,我从来没有得到解决方案来解决这个问题,现在请告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:-1)

如果我理解正确,您想显示验证错误吗?

在View文件中,输入以下内容:

简短的IF声明表格:

<?php if ( validation_errors() ) :?>
<div>
   <?php echo $validation_errors();?>
</div>
<?php endif;?>

常规If-Statement表格:

<?php if ( validation_errors() ) {?>
<div>
   <?php echo $validation_errors();?>
</div>
<?php }?>

Validation Errors Doc

相关问题