实例
<?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>'; } public function hello($name = 'ThinkPHP5') { return 'hello,' . $name; } public function example1() { $name='KimJin'; //使用视图方法:display()不通过模板,直接渲染内容,支持HTML标签 // return $name; // return view::display($name); // return view::display('我的英文名是:'.$name); // return view::display('我的英文名是:<span style="color:red">'.$name.'</span>'); //fetch(模板表达式) //fetch('模板表达式',[模板变量数组],[模板配置数组]) // return View::fetch('index@index/index1',['data'=>$data]); // return View::fetch('ep1',['name'=>'Kimjinx']); // return $this->view->fetch('ep1',['name'=>'Kimjinx2']); // return $this->fetch('ep1',['name'=>'Kimjinx3']); //助手函数view() return view('ep1',['name'=>'Kimjinx4']); } public function example2() { //模板赋值 //1.assign $name='Jack-jonash'; $this->view->assign('name',$name); // return $this->view->fetch('ep2'); //2.直接传参 // return $this->fetch('ep2',['name'=>'Kimjinx3']); //3对象方法 $this->view->name='Tomas'; return $this->view->fetch('ep2'); } public function example3() { //模板的替换 $this->view->name='KingKong'; return $this->filter(function($content){ return str_replace('KingKong', 'Dargon', $content); })->fetch('ep2'); } //模板的布局 public function example4() { $this->view->name='小明'; return $this->view->fetch('ep4'); } //模板的继承 public function example5() { return $this->view->fetch('ep5'); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
模板的布局
模板的继承