敛林彪瞪东剁迹滇计豌充梳另
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //1.单独赋值 $this->assign('siteName','萌面人资料铺'); //2.批量赋值 $this->assign([ 'name'=>'萌面人', 'course'=>'ThinkPHP5' ]); //3.渲染模板 return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>网站名称:<span style="color:red">{$siteName}</span></p> <p>我的名字:<span style="color:red">{$name}</span></p> <p>我学习的课程:<span style="color:red">{$course}</span></p>
执行:
给fetch()传递参数
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //1.单独赋值 $this->assign('siteName','萌面人资料铺'); //3.渲染模板 return $this->fetch('index',[ 'name'=>'萌面人', 'course'=>'ThinkPHP5' ]); } }
执行:
display()
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //1.单独赋值 $this->assign('siteName','萌面人资料铺'); return $this->display('姓名:{$name},学习的课程:{$course}',[ 'name'=>'萌面人', 'course'=>'thinkphp5' ]); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>网站名称:<span style="color:red">{$siteName}</span></p> <p>我的名字:<span style="color:red">{$name}</span></p> <p>我学习的课程:<span style="color:red">{$course}</span></p>
执行:
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //1.模板赋值 $this->view->name="萌面人"; $this->view->course="php"; //渲染模板 return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>我的名字:<span style="color:red">{$name}</span></p> <p>我学习的课程:<span style="color:red">{$course}</span></p>
执行:
直接使用模板类:
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index { public function index() { //1.模板赋值 \think\View::instance()->name="萌面人"; \think\View::instance()->course="HTML5"; //渲染模板 return \think\View::instance()->fetch(); } }
执行:
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index { public function index() { //1.模板赋值 \think\View::instance()->name="萌面人"; \think\View::instance()->course="HTML5"; \think\View::share('siteName','萌面人资料铺'); //渲染模板 return \think\View::instance()->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>我的名字:<span style="color:red">{$name}</span></p> <p>我学习的课程:<span style="color:red">{$course}</span></p> <p>我的网站名称:<span style="color:red">{$siteName}</span></p>
执行:
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index { public function index() { return view('index',[ 'name'=>'mengmianren', 'course'=>'jquery', 'siteName'=>'卖萌资料铺' ]); } }
执行:
东苯芯樊深济衔抖临腿氖坪脓