ThinkPHP5.1模板赋值和输出

<?php

//index/controller/Demo1.php

namespace app\index\controller;
use think\Controller;
use think\facade\View;
class Demo1 extends Controller
{


public function index()
{
//直接输出内容到页面,不通过模板
$content = '<h3>微语录www.top789.cn</h3>';
// return $this->display($content);
return $this->view->display($content);//推荐
// return View::display($content);
}
//使用视图输出
public function test()
{
//1、普通变量
$this->view->assign('name','Sam');
$this->view->assign('age','99');
//批量
$this->view->assign(['sex'=>'男','salary'=>'9999']);
//2、数组
$this->view->assign('arr',[
'id'=>'1',
'name'=>'SamC',
'sex'=>'男',
]);
//3、对象
$obj=new \stdClass();
$obj->title='手机';
$obj->price='价格';
$this->view->assign('info',$obj);
//常量
define('SITE_NAME', '微语录');


return $this->view->fetch();


}
}


---------对应的模板index/view/demo1/test.html


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
{$name}<br />
{$age}<br />
{$sex}<br />
<hr>
{//输出数组}
{$arr.id}
{$arr.name}
{$arr['sex']}
<hr>
{//输出对象}
{$info->title}
{$info->price}
<hr>
{//输出常量}
{$Think.const.SITE_NAME}
<hr>
{//输出php系统常量}
{$Think.const.PHP_VERSION}
{$Think.const.PHP_OS}
<hr>
{//输出系统变量}
{$Think.server.php_self}
{$Think.server.get.name}
<hr>
{//输出系统配置里参数}
{$Think.config.database.hostname}
<hr>
{//获取请求变量}
{$Request.get.title}<br />
{$Request.param.title}<br />
{$Request.path}<br />
{$Request.root}<br />
{$Request.root.true}<br />
{$Request.action}<br />
</body>

</html>

---------------

public function local($tag)
{
  if ($tag=='go') {
            //设置成功后跳转页面的地址,默认的返回页面是$_SERVER['HTTP_REFERER']
            $this->success('操作成功', 'Index/index');
        } else {
            //错误页面的默认跳转页面是返回前一页,通常不需要设置
            $this->error('操作失败');
        }


}

猜你喜欢

转载自blog.csdn.net/samcphp/article/details/79662321