注意事项:
1、在当前子模板中,只能定义区块而不能定义其他的模板内容,否则将会直接忽略
Paste_Image.png2、thinkphp3.2.3默认不支持<block>标签嵌套
项目中一个使用实例
1、Public目录下
-
base.html
<!DOCTYPE html>
<html lang="en">
<!-- 引入公共头部文件 -->
<block name="include_header">
<include file="Public:head" />
</block>
<body>
<!-- 搜索 -->
<include file="Public:top" />
<!-- 内容区 -->
<block name="content">
</block>
<!-- 引入公共头部文件 -->
<block name="include_footer">
<include file="Public:footer" />
</block>
</body>
</html>
-
top.html
<div class="search-content">
<div class="search-con">
<form method="post" action="#">
<h3>搜索感兴趣的内容</h3>
<p>
<input type="text" placeholder="请输入搜索内容" />
</p>
<p>
<input id="btn" type="submit" value="确定" />
</p>
</form>
</div>
</div>
<header class="head" class="clearfix">
<div class="logo">![](/Public/Home/images/logo.png)</div>
<div class="sidenav clearfix">
<!-- 导航栏 -->
<block name='include_nav'>
<include file="Public:nav" />
</block>
<div class="search">![](/Public/Home/images/search.png)</div>
<div class="subnav">![](/Public/Home/images/nav.png)</div>
</div>
</header>
top.html文件里也有个block区块
<block name='include_nav'><include file="Public:nav" /></block>
-
nav.html
<nav>
<ul>
<li class="current"><a class="cur" href="index.html">首页</a><i></i></li>
<li class="current"><a class="cur" href="index.html">关于我们</a><i></i></li>
<li class="current"><a class="cur" href="index.html">招贤纳士</a><i></i></li>
<li class="current"><a class="cur" href="index.html">联系我们</a><i></i></li>
</ul>
</nav>
-
head.html 和 footer.html文件只是引入公共css、js文件
2、其他视图页面继承基础模版
-
Home/ContactController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
class ContactController extends Controller {
public function index(){
$config = C('nav_info');
$current_nav_id = $config[9]['id'];
$banner_img = M('nav')->where(array('id'=>$current_nav_id))->getfield('pic');
$contact_info = M('contact')->find();
$this->assign(
array(
'nav_id' => $current_nav_id,//渲染当前页面的id
'banner_img' => $banner_img,
'contact_info' => $contact_info,
));
$this->display();
}
}
-
Home/View/Contact/index.html
<extend name="Public:base" />
<block name='content'>
<div class="banner">
![](/Public/{$banner_img})
</div>
<div class="contact">
<h2>![](/Public/Home/images/contact-tit.png)</h2>
<h3>{$contact_info.contact_name}</h3>
<div class="hr02"></div>
<div>
{$contact_info.contact_content}
</div>
</div>
</div>
</block>
网友评论