当前位置: 移动技术网 > IT编程>开发语言>PHP > thinkphp5.1框架模板赋值与变量输出示例

thinkphp5.1框架模板赋值与变量输出示例

2020年06月14日  | 移动技术网IT编程  | 我要评论

喜爱夜蒲2完整版,涩谷果步,昆明三对

本文实例讲述了thinkphp5.1框架模板赋值与变量输出。分享给大家供大家参考,具体如下:

demo.php

namespace app\index\controller;
use think\controller;
use think\facade\view;
class demo extends controller
{
  public function test1()
  {
    //直接将内容输出到页面,不通过模板
    $con = '<h3 style="color:red">php</h3>';
    return $this->dieplay($con);
    return $this->view->display($con);
    return view::display($con);//静态代理
  }
 
  //使用视图将数据进行输出:fetch()
  public function test2()
  {
    //模板变量赋值:assign()
    //1、普通变量
    $this->view->assign('name','zhang');
    $this->view->assign('age',23);
 
    //批量赋值
    $this->view->assign([
      'sex' => '男',
      'salary' => 1200
    ]);
 
    //2、array
    $this->view->assign('goods',[
      'id' => 1,
      'name' => '手机',
      'model' => 'meta10',
      'price' => 3000
    ]);
 
    //3、object
    $obj = new \stdclass();
    $obj->course = 'php';
    $obj->lecture = 'zhang';
    $this->view->assign('info',$obj);
 
    //4、const(系统常量)
    define('site_name','php');
 
    //在模板中输出数据
    //模板默认的目录位于当前模块的view目录,模板文件默认位于以当前控制器命名的目录中
    return $this->view->fetch();
  }
}

创建视图文件夹(application\index\view)

创建模板(application\index\view\demo\test2.html)

输出变量:

{$name}<br>
{$age}<br>
{$sex}<br>
{$salary}<br>
<hr>
{//输出数组}
{$goods.id}<br>
{$goods.name}<br>
{$goods['model']}<br>
{$goods['price']}<br>
<hr>
{//输出对象}
{$info->course}<br>
{$info->lecture}<br>
<hr>
{//输出常量}
{$think.const.site_name}<br>
<hr>
{//输出php系统常量}
{$think.const.php_version}<br>
{$think.const.php_so}<br>
<hr>
{//输出系统变量}
{$think.server.php_self}<br>
{$think.server.session.id}<br>
{$think.server.get.name}<br>
{$think.server.post.name}<br>
<hr>
{//输出数据库配置}
{$think.config.database.hostname}<br>
<hr>
{//输出请求变量}
{$request.get.name}<br>
{$request.param.name}<br>
{$request.path}<br>
{$request.root}<br>
{$request.root.true}<br>
{//查询当前控制器}
{$request.controller}<br>
{//查询当前方法}
{$request.action}<br>
{//查询域名}
{$request.host}<br>
{//查询ip}
{$request.ip}<br>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网