CI控制器調用內部方法並載入相應模板的做法,ci控制器

來源:互聯網
上載者:User

CI控制器調用內部方法並載入相應模板的做法,ci控制器

當我開啟連結:http://localhost/3g/index/open/a/b?from=timeline後,判斷連結中的from是否等於timeline,如果等於timeline,那麼就調用控制器內部的timeline方法,並載入timeline.php這個模板。

class Index extends CI_Controller {    function __construct() {        parent::__construct();    }    public function open($a=0,$b=0) {        if ($_GET['from'] == 'timeline') {            $this -> timeline($a);            die;        } else {            $data['b'] = $b;            $this -> load -> view('3g/index/open',$data);        }    }    public function timeline($a=0) {        $data['a'] = $a;        $this -> load -> view('3g/index/timeline',$data);    }}
此時輸出的頁面一片空白,並沒有載入timeline.php這個模板,正確的做法是:

class Index extends CI_Controller {    function __construct() {        parent::__construct();    }    public function open($a=0,$b=0) {        if ($_GET['from'] == 'timeline') {            $this -> timeline($a);            die;        } else {            $data['b'] = $b;            $this -> load -> view('3g/index/open',$data);        }    }    public function timeline($a=0) {        $data['a'] = $a;        echo $this -> load -> view('3g/index/timeline',$data,true);die;    }}

擷取視圖內容

view函數第三個可選參數可以改變函數的行為,讓資料作為字串返回而不是發送到瀏覽器。如果想用其它方式對資料進一步處理,這樣做很有用。如果將view第三個參數設定為true(布爾)則函數返回資料。view函數預設行為是 false, 將資料發送到瀏覽器。如果想返回資料,記得將它賦到一個變數中:

$string = $this->load->view('myfile', '', true);

例子:有些情況下,你並不想直接輸出視圖,而是僅僅想得到視圖的內容以備後用。那麼可以參考如下代碼。

<?phpclass Blog extends CI_Controller { function index() { $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands'); $data['title'] = "My Real Title"; $data['heading'] = "My Real Heading"; $buffer = $this->load->view('blogview', $data, true); }}?>

view方法中的第三個參數表示不輸出視圖,而只是將結果返回給一個變數。




PHP CI架構開發,調用模板的時沒顯示內容

你有右鍵 查看下源碼 看是空白還是什嗎?
把模板檔案裡的 <?php?> 去了
 
模板加固做法,注意是怎做的,不是那些方案比如梁的側面模板拿鋼

你還能再說得麻煩點麽?來句簡單明了的
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.