批改状态:未批改
老师批语:
主题:
实例演示:
模板赋值;
模板内容过滤与替换;
模板动态布局;
模板继承技术。
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
// 二、模板赋值
public function demo2() {
// 1.assign('模板变量名', 值)方法
$name = '小明2';
// $this->view->assign('my_name', $name);
// $this->view->assign('type', '哈哈');
// 2.传参赋值:fetch('模板', [参数数组])
$this->fetch('demo2',['my_name'=>$name,'type'=>'超级跑车2']);
// 3.对象赋值:
// $this->view->my_name = $name;
// $this->view->type = '超级跑车';
return $this->view->fetch();
}
}点击 "运行实例" 按钮查看在线实例
我的名字是:<p style="color:red;">{$my_name}</p>
我喜欢:<span style="color:red;">{$type}</span>点击 "运行实例" 按钮查看在线实例
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
// 三、模板内容过滤和替换
public function demo3() {
// 5.1版本之后删除了内置的替换功能,采用config/template.php进行配置
$this->fetch('demo3',['name'=>'小明3','type'=>'超级跑车2']);
// 使用str_replace()将内容替换成空字符串相,当于过滤了该内容
$filter = function($content) {
return str_replace('小明3', '', $content);
};
// return $this->fetch();
return $this->filter($filter)->fetch();
}
}点击 "运行实例" 按钮查看在线实例
我的名字是:<p style="color:red;">{$name}</p>
我喜欢:<span style="color:red;">{$type}</span>点击 "运行实例" 按钮查看在线实例
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
// 四、模板布局
public function demo4() {
// 1.全局开启模板布局(config\tamplate.php)
// 2.用标签配置模板布局:不依赖全局配置
// {layout name="布局模板名" /}:添加布局
// {__NOLAYOUT__}:关闭全局布局
// 3.动态布局
// (1)不需要在模板配置文件中进行任何配置
// (2)不需要在当前模板中添加任何标签
// 动态开启全局布局
// $this->view->engine->layout(true);
// 注意在模板文件layout.html中名称(__TEXT__)要对应
// $this->view->engine->layout('layout', '{__content__}');
$this->view->engine->layout('layout', '{__TEXT__}');
// 默认的模板文件是引入view\layout
return $this->view->fetch();
// return $this->view->fetch('demo4');
}
}点击 "运行实例" 按钮查看在线实例
<style type="text/css">
.main {
width: 600px;
height: 500px;
margin: auto;
background-color: skyblue;
text-align: center;
line-height: 500px;
}
</style>
<div class="main">主体内容</div>点击 "运行实例" 按钮查看在线实例
{include file="public/header" /}
{__TEXT__}
{include file="public/footer" /}点击 "运行实例" 按钮查看在线实例
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
// 五、模板继承
public function demo5() {
// view\base.html:基础模板,供其他模板继承
// base.html内容全部要用标签{block}定义
return $this->view->fetch();
}
}点击 "运行实例" 按钮查看在线实例
{//继承基础模板base.html}
{extend name="base" /}
{block name="main"}
<p style="text-align: center;">继承后重写主体部分</p>
{/block}
{block name="car"}
{//__BLOCK__常量引用父模板的内容}
<p style="text-align: center;">name="car"的重写内容, {__BLOCK__}</p>
{/block}
{block name="fly"}
{//这里内容为空可将父模板中的相应内容清空}
{/block}点击 "运行实例" 按钮查看在线实例
{//基础模板(父模板)中只允许出现block标签}
{block name="header"}
{include file="public/header" /}
{/block}
{block name="main"}
主体部分
{/block}
{block name="car"}
<br>name="car"的原始内容
{/block}
{block name="fly"}
name="fly"的原始内容
{/block}
{block name="footer"}
{include file="public/footer" /}
{/block}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号