簡(jiǎn)而言之,一個(gè)視圖就是一個(gè)html網(wǎng)頁(yè),或是網(wǎng)頁(yè)的部分,如頭部,底部,側(cè)邊欄等等。
事實(shí)上,如果你需要這種層次類(lèi)型,視圖可以很靈活的嵌入到其他視圖中。
視圖從不直接調(diào)用,必須被一個(gè)控制器來(lái)調(diào)用。記住,在一個(gè) MVC 框架中,控制器扮演著交通警察的角色,那么,他有責(zé)任去取回某一特定的視圖。
創(chuàng)建視圖
使用你的文本編輯器,創(chuàng)建一個(gè)名為 blogview.php 的文件,寫(xiě)入以下代碼:<html><head><title>My Blog</title></head><body><h1>Welcome to my Blog!</h1></body></html>
然后保存文件到 application/views/ 文件夾。
$this->load->view('name');上面的 name 便是你的視圖文件的名字。注意:.php 文件的擴(kuò)展名(后綴名)沒(méi)有必要專(zhuān)門(mén)寫(xiě)出,除非你使用了其他的擴(kuò)展名。
<?phpclass Blog extends CI_Controller{ function index(){ $this->load->view('blogview'); }}?>
如果你使用先前你用的 URL 瀏覽你的網(wǎng)站,你將會(huì)看到你的新視圖.
URL 與下面的類(lèi)似:example.com/index.php/blog/
<?phpclass Page extends CI_Controller { function index() { $data['page_title'] = 'Your title'; $this->load->view('header'); $this->load->view('menu'); $this->load->view('content', $data); $this->load->view('footer'); }}?>在上面的例子中,我們使用了“動(dòng)態(tài)添加數(shù)據(jù)”,你將在下面看到。
$this->load->view('folder_name/file_name');
給視圖添加動(dòng)態(tài)數(shù)據(jù)
數(shù)據(jù)通過(guò)控制器以一個(gè)數(shù)組或是對(duì)象的形式傳入視圖 , 這個(gè)數(shù)組或?qū)ο笞鳛橐晥D載入函數(shù)的第二個(gè)參數(shù) .
下面便是使用數(shù)組的示例:
function testView(){ $data = array( 'title' => 'My Title', 'heading' => 'My Heading', 'message' => 'My Message' ); $this->load->view('blogview', $data);}
$data = new Someclass();$this->load->view('blogview', $data);
<?phpclass Page extends CI_Controller { function index() { $data['title'] = 'Your title'; $data['message'] = 'Your message'; $this->load->view('header',$data); $this->load->view('content'); $this->load->view('footer'); }}?>
<?php class Blog extends CI_Controller { function index() { $data['title'] = "My Real Title"; $data['heading'] = "My Real Heading"; $this->load->view('blogview', $data); } } ?>
<html> <head> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> </body> </html>然后使用你先前用過(guò)的URL載入頁(yè)面,你將看到變量已經(jīng)被替換。
創(chuàng)建循環(huán)
你傳入視圖文件的數(shù)據(jù),不僅僅局限于簡(jiǎn)單的變量。你可以傳遞多維數(shù)組。例如:你從數(shù)據(jù)庫(kù)里面取出數(shù)據(jù)就是典型的多維數(shù)據(jù)。<?php class 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"; $this->load->view('blogview', $data); }}?>
現(xiàn)在打開(kāi)你的視圖文件,創(chuàng)建一個(gè)循環(huán):
<html> <head><title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> <h3>My Todo List</h3> <ul> <?php foreach ($todo_list as $item):?> <li><?php echo $item;?></li> <?php endforeach;?> </ul> </body> </html>
注意: 上面的例子中我們使用PHP替代語(yǔ)法。
view函數(shù)第三個(gè)可選參數(shù)可以改變函數(shù)的行為,讓數(shù)據(jù)作為字符串返回而不是發(fā)送到瀏覽器。如果想用其它方式對(duì)數(shù)據(jù)進(jìn)一步處理,這樣做很有用。如果將view第三個(gè)參數(shù)設(shè)置為true(布爾)則函數(shù)返回?cái)?shù)據(jù)。
view函數(shù)缺省行為是 false, 將數(shù)據(jù)發(fā)送到瀏覽器。如果想返回?cái)?shù)據(jù),記得將它賦到一個(gè)變量中:
$string = $this->load->view('myfile', '', true);
<?php class 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); }} ?>
下一篇 php字符串中查找字符