• ThinkPHP5.1模板赋值和输出


    <?php//index/controller/Demo1.php
    namespace appindexcontroller;
    use thinkController;
    use thinkfacadeView;
    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('操作失败');
    }

    }

  • 相关阅读:
    阿里巴巴公布合伙人名单,董建华成为独董,俞永福未进入合伙人
    android cookie
    在Android的webview中定做js的alert,confirm和prompt对话框的方法
    浅谈WebView的使用 js alert
    Android的移动存储之SharedPreferences
    [android]-如何在向服务器发送request时附加已保存的cookie数据
    layout_weight
    我们是如何认识世界的
    挣钱和花钱
    时刻牢记“我是谁、为了谁、依靠谁” 始终践行党的群众观点和群众路线
  • 原文地址:https://www.cnblogs.com/samphp/p/8597910.html
Copyright © 2020-2023  润新知