ThinkPHP5.1模板赋值和输出

本文介绍了ThinkPHP5.1框架中控制器方法`Demo1`的`local`函数,用于处理操作成功或失败的场景。当$tag为'go'时,使用`success`方法跳转到'Index/index'页面并显示'操作成功'信息;否则,通过`error`方法显示'操作失败'的错误提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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('操作失败');
        }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值