在做网站的时候,每个网站都有头部和尾部,也就是菜单栏和页脚,网站的各个子网页的头部和尾部基本就是一样的,所以tp框架提供了一种模板继承的方法:

1、首先在View的Main文件夹下建立一个base.html页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><block name="title">无标题文档</block></title>
<style>
#head{ width:100%; height:100px; background-color:red}
#footer{ width:100%; height:50px; background-color:black}
</style>
<block name="tou"></block><!--头部-->
</head> <body> <div id="head">
<foreach name="arr" item="vo"><!--循环遍历,从数据库读数据-->
<span>{$vo.lbname}</span>
</foreach>
</div>
<block name="nr"></block>
<div id="footer"></div><!--尾部-->
</body>
</html>

2、做操作方法: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("leibie");
$arr = $m->select();
$this->assign("arr",$arr); }
}

3、在View文件夹的Main文件夹下新建一个test.html文件:

<extend name="base"/><!--调用模板-->
<block name="title">子页面</block>
<block name="tou">
<style>
#nr{ width:100%; height:400px; background-color:yellow}
</style>
</block>
<block name="nr">
<div id="nr"></div>
</block>

看一下下效果:

4、删除和修改:

在View文件夹下的Main文件夹内新建一个mains.html文件:

<extend name="base"/><!--继承模板-->
<block name="nr">
<table width="100%" border="1">
<tr>
<td>代号</td>
<td>名称</td>
<td>系列</td>
<td>油耗</td>
<td>价格</td>
<td>操作</td>
</tr>
<foreach name="attr" item="v"><!--循环遍历出表中内容-->
<tr>
<td>{$v.code}</td>
<td>{$v.name}</td>
<td>{$v.brand}</td>
<td>{$v.oil}</td>
<td>{$v.price}</td>
<td><a href="__CONTROLLER__/del/code/{$v.code}">删除</a>
<a href="__CONTROLLER__/upd/code/{$v.code}">修改</a>
</td>
</tr>
</foreach>
</table>
</block>

依然在MainController的控制器里做操作方法:

<?php
namespace Ceshi\Controller;
use Think\Controller;
class MainController extends Controller
{ public function test(){
$this->base();
$this->show();
}
public function base(){//这样可以调用模板中连接数据库部分
$m = M("leibie");
$arr = $m->select();
$this->assign("arr",$arr); }
public function mains(){
$m = M("car");
$arr = $m->select();
$this->assign("attr",$arr);
$this->base();
$this->show();
}
public function del($code){
$m = M("car");
if($m->delete($code)){
$url = U("mains");
$this->success("删除成功!",$url);//第二个参数,表示返回的路径;第三个参数:表示停留时间
}
else{
$this->error("删除失败!");
}
}
public function upd(){
$m = M("car");
$code = $_GET["code"];
$attr = $m->find($code);
$this->assign("attr",$attr);
if(empty($_POST)){
$this->show();
}
else{
$m->create();
$m->save(); }
}
}

在View下的Main下建立一个upd.html文件:

<extend name="base"/>
<block name="nr">
<form action="__ACTION__" method="post">
<div><input type="hidden" name="Code" value="{$attr.code}"/></div>
<div>名称:<input type="text" name="Name" value="{$attr.name}"/></div>
<div>系列:<input type="text" name="Brand" value="{$attr.brand}"/></div>
<div>油耗:<input type="text" name="Oil" value="{$attr.oil}"/></div>
<div>价格:<input type="text" name="Price" value="{$attr.price}"/></div>
<input type="submit" value="修改"/>
</form>
</block>

看一下效果:

点击删除c002:

点击修改c003价格为40:

最新文章

  1. JS基础学习(二)
  2. 网页的title左边的小图片怎么添加
  3. Tableview 优化Cell的复用机制01
  4. Xcode安装的推送证书所在目录
  5. SELinux开启与关闭
  6. Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
  7. MVC JsonResult
  8. Elasticsearch 默认配置 IK 及 Java AnalyzeRequestBuilder 使用
  9. java报错排解
  10. 微服务架构 - 解决Docker-Compose服务编排启动顺序问题
  11. WebService访问oracle数据库本地调试
  12. SQL Server数据库开发的二十一条军规
  13. Op-level的快速算法
  14. python3学习笔记七(字典)
  15. David McCullough, Jr.为韦斯利高中毕业生演讲〈你并不特别〉
  16. js函数 test.caller 谁在调用test函数
  17. 比较全git的.ignore文件配置
  18. Laravel返回不重复的某个字段信息列表
  19. Leetcode题库——26.删除排序数组中的重复项
  20. Zookeeper安装和配置详解

热门文章

  1. py基础
  2. exec 命令
  3. SCUT - 77 - 哈利波特与他的魔法杖 - 线段树
  4. 回溯--- Permutations
  5. SQLServer死锁查询
  6. (转)Centos7下杀毒软件clamav的安装和使用
  7. AMBS
  8. python struct中的pack unpack
  9. Less 混合(mixin)
  10. sqlmap POST注入