摘要:<?php namespace app\index\controller; use think\facade\View; use think\Controller; class Index extends Controller { public function i
<?php
namespace app\index\controller;
use think\facade\View;
use think\Controller;
class Index extends Controller
{
public function index()
{
// return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改(2006-2018) - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
return '<h3>欢迎来到PHP中文网学习<span style="color:red">ThinkPHP5.1</span>框架开发</h3>';
}
//模版渲染
public function demo1(){
$name="minlei";
//return View::fetch('demo1',['name'=>$name]);
return View::fetch('demo1',['name'=>'peter']);
//return $this->view->fetch('demo1',['name'=>'peter']);
}
public function demo2(){
//模板赋值:
//1. assign()
//使用assign()必须要调用View类,以后我们统一使用Controller来调用
//2.传参方式: fetch()或view()刚才已经演示过了
//3.对象方式:底是通过Controller中的二个魔术方法来实现视图类的数据注入的
$this->view->name = 'peter';
// return $this->fetch('demo2');
$name='peter';
$this->view->assign('name',$name);
return $this->view->fetch();
}
//模版过滤与替换
public function demo3(){
//如果是按默认规则创建的模板文件,则模板文件名可以省略
//tp51也之前版本相比,直接删除了模板字符串替换功能,而改为模板配置参数来实现
//config/template.php: 'tpl_replace_str' => [''=>''],
//不过,我建议大家在控制器使用filter()直接进行过滤替换,更简洁
//将模板中的peter zhu换成:朱老师
$this->view->assign('name','peter');
$filter=function($content){
return str_replace('peter','朱老师',$content);
};
return $this->filter($filter)->fetch();
}
//模板布局
/**
* 一,全局配置
* 1.config/temaplate
* 二,模板标签进行配置
* 1,不依赖全局配置,在模板中直接用标签进行控制
* 2{layout name="布局模板名" /}
* 3关闭布局{__NOLAYOUT__}
* 三,动态配置
* 1不需要在模板配置文件中进行任何配置
* 2不需要在当前模板中添加任何标签
*
*/
public function demo4(){
//开启布局
//$this->view->engine->layout(true);
//$this->view->engine->layout(false);
//$this->view->engine->layout('layout','{__TEXT__}');
//return $this->view->fetch();
return $this->view->engine->layout(true)->fetch('index\demo4');
}
//模板继承
public function demo5(){
/*
* 1.view/base.html:基础模板,供其它子模板进行继承\
* 2.base.html内容全部要用标签{block}进行定义
*/
return $this->view->fetch();
}
}base.html
{block name="header"}
{include file="public/header" /}
{/block}
{block name="main"}主体部分{/block}
{block name="course"}课程名称:{/block} <br>
{//site区块,在子模块中未进行重写,会原样显示}
{block name="site"}PHP中文网{/block} <br>
{block name='name'}朱老师{/block}
{block name="footer"}
{include file="public/footer" /}
{/block}
{//<h3>标签中的内容会原样输出到子模板中}
<h3>我是基础模板中的标题内容</h3>
{/*
1.基础模板中,应该只有block标签,而不应该有其它的标签;
2.写在block标签之外内容,会原样输出到子模板中
*/}layout.html
{include file="public/header" /}
{__TEXT__}
{include file="public/footer" /}
批改老师:韦小宝批改时间:2018-11-20 16:17:54
老师总结:嗯!写的很棒!代码都很完整!不要骄傲!继续加油吧!!