zoukankan      html  css  js  c++  java
  • 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('操作失败');
    }

    }

  • 相关阅读:
    dfs模板
    24点
    个人阅读三
    个人阅读作业2:关于项目经历的心得
    代码复审
    Pair Project1:电梯控制程序
    第二次个人项目——阅读经典教材
    THE First Individual Project
    个人阅读作业3
    个人阅读作业2
  • 原文地址:https://www.cnblogs.com/samphp/p/8597910.html
Copyright © 2011-2022 走看看