dongsha9208 2014-06-26 13:51
浏览 58

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

I have a template design for each page and it is working without any issues, now i have a problem with form validation and template.

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');
 }
}

So my question is how do i can call same view (within one controller) for multiple time when it is already loaded with template?

EDITED for more clarity

My template file is just single file ( not separated like header, footer) will have all header information and footer information with common style sheet and script files. I am having

<?php $content ?>

variable name inside the template to pass my content from controller.

for example see 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

This page is having form validation, if my form validation is FALSE, how do i can reload the template page with validation error?

I have tried so many way, please check my option1, 2, 3, i never got a solution to solve this, now please tell me how do i can solve this?

  • 写回答

1条回答 默认 最新

  • doubei2231 2014-06-26 14:56
    关注

    If I understand correctly, you want to display the validation errors?

    Inside your View file, put the following:

    Short IF-Statement Form:

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

    Regular If-Statement Form:

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

    Validation Errors Doc

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站