7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数

敛林彪瞪东剁迹滇计豌充梳另

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第1张

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>

执行:

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第2张


7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第3张

给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'
        ]);
    }
}

执行:

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第4张


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>

执行:

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第5张


7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第6张

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>

执行:

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第7张

直接使用模板类:

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();
    }
}

执行:

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第8张


7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第9张

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>

执行:

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第10张


7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第11张

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'=>'卖萌资料铺'
        ]);
    }
}

执行:

7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第12张


7-3 ThinkPHP5模板变量赋值的五种方法:方法/传参/对象/静态/助手函数第13张

东苯芯樊深济衔抖临腿氖坪脓