tp框架---View視圖層---模板繼承(舉例說明)


當我們做動態頁面時,我們會發現一個網站的頭部和尾部是相同的,那么我們如何用tp框架來做模板呢 ?

先看一下注意事項:

(1)每個區塊由<block></block>標簽組成

(2)子模板中使用extend標簽繼承模板

(3)注釋語法:{/* 注釋內容 */ } {// 注釋內容 } 

一、看一下基本的模板繼承

(1)先做出模板頁面  Ceshi/View/Main/base.html

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<!--放標題-->
		<title><block name="bt">標題</block></title>
		<style>
			*{padding: 0px;margin: 0px;}
			#head{width: 100%;height: 100px;background-color: azure;}
			#foot{width: 100%;height: 100px;background-color: gray;}
		</style>
		<!--用於放js、css樣式-->
		<block name="tou"></block>
	</head>
	<body>
		<div id="head">
			 <h1>這是頭部</h1>
			 <!--從數據庫讀取數據-->
			<foreach name="arr" item="vo" >   
				<span>{$vo.name}</span>
			</foreach>			
		</div>		
		<!--這是需要依據頁面變化的內容-->
		<block name="nr"></block>
		
		<div id="foot">
			 <h1>這是尾部</h1>
		</div>
	</body>
</html>

  (2)做操作方法Ceshi/Controller/MainController.class.php

<?php
namespace Ceshi\Controller;
use Think\Controller;
class MainController extends  Controller
{	

   	//模板的繼承
   	public function test(){  		
   		//這樣可以調用模板中連接數據庫部分
        $this->base();
   		$this->show();
   	}
    	
//當模板頁面鏈接數據庫時 public function base(){ $m=M("users"); $arr = $m->select(); $this->assign("arr",$arr); } }

  (3)做子頁面   Ceshi/View/Main/test.html

<!--調用模板-->
<extend name="base" />

<block name="bt">子頁面</block>
<block name="tou">
	<style type="text/css">
		#nr{width: 100%;height: 400px;background-color: yellow;	}	
	</style>
</block>
<block name="nr">
	<div id="nr">
		<h1>我是中間內容</h1>
	</div>
</block>

  效果圖(樣式有點簡單)

 

 注意事項:當模板頁面有鏈接數據庫的數據時怎樣做

 

(二)舉例:刪除和修改

(1)先做顯示頁面    Ceshi/View/Main/mains.html

<extend name="base" />
<block name="bt">shanchu</block>
<block name="tou">
	<style type="text/css">
		/*#nr{width: 100%;height: 400px;background-color: yellow;	}*/
		#aa{height: 400px;width: 100%;background-color: yellow;}	
	</style>
</block>
<block name="nr">
	<div id="aa">
	<table width="100%" border="1" cellpadding="0" cellspacing="0">
		<tr>
			<td>代號</td>
			<td>書名</td>
			<td>單價</td>
			<td>庫存</td>
			<td>時間</td>
			<td>操作</td>
		</tr>
		
		<!--name表示數組名稱  item表示遍歷出來的每一個小數組-->
			<foreach name="attr" item="v" >   
		<tr>
			<td>{$v.bkid}</td>
			<td>{$v.bkname}</td>
			<td>{$v.bkprice}</td>
			<td>{$v.bkstock}</td>
			<td>{$v.time}</td>
			<!--用get方式傳值-->
			<td><a href="__CONTROLLER__/del/bkid/{$v.bkid}">刪除</a>
			<a href="__CONTROLLER__/update/bkid/{$v.bkid}">修改</a></td>
		</tr>
			</foreach>
			
	</table>
</div>
</block>

  

(2)做操作方法Ceshi/Controller/MainController.class.php

<?php
namespace Ceshi\Controller;
use Think\Controller;
class MainController extends  Controller
{	

   	
   	
   	public  function mains(){
   		$m = M("book");
   		$arr = $m->select();
   		$this->assign("attr",$arr);
   		$this->show();
   	}
   	  
   	  	
    public  function del($bkid){
    	//$bkid 以形參的方式傳值
   		$m = M("book");
   		$url = U("mains");
   		if($m->delete($bkid)){
   			//第二個參數,表示返回的路徑;第三個參數:表示停留時間
   			$this->success("刪除成功",$url);
   		} else{
   			//不寫第二個參數,默認返回上一個頁面
   				$this->error("刪除失敗");
   		}
   	}
   	
   	public  function update(){
   		
   	  	$m = M("book");	
   	  	//get方式取到值
   	  	$bkid = $_GET["bkid"];
   	  	$attr = $m->find($bkid);
   	  	//注冊變量
   		$this->assign("attr",$attr);
   			
   		if(empty($_POST)){
   			
   			$this->show();
   			
   		}else{
   			//自動獲取表單數據
   			$m->create();  			
   			$m->save();
   			
   		}
   	}
   
      
}

  (3)修改頁面 Ceshi/View/Main/update.html

<extend name="base" />
<block name="bt">修改</block>
<block name="tou">
	<style type="text/css">
		#nr{width: 100%;height: 400px;background-color: yellow;}		
	</style>
</block>
<block name="nr">
<form method="post" action="__ACTION__">
	<input type="hidden" name="bkid" value="{$attr.bkid}" /><br />
	書名:<input type="text" name="bkname" value="{$attr.bkname}" /><br />
	單價:<input type="text" name="bkprice"  value="{$attr.bkprice}"/><br />
	庫存:<input type="text" name="bkstock"  value="{$attr.bkstock}"/><br />
	<input type="submit" value="修改"/>
</form>
</block>

  看一下效果:

 

 點擊刪除bkid=33;

 

 原頁面bkid=33已經被刪除

 

點擊修改bkid=22;

點擊修改提交數據,重新刷新頁面,會發現單價和粗存已經被修改了

 

 這樣,tp框架的刪除和修改就完成了,至於用ajax方式還是tp框架方式寫,哪個更方便就用哪個唄~~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM