thinkphp 模板變量輸出替換和賦值


一.變量輸出的幾個方法

<?php

namespace app\index\controller;

use http\Params;
use think\Config;
use think\Controller;
use think\Request;
use think\View;

import('filter', APP_PATH . 'common', ".php");

class Index extends Controller
{
    /**
     * @param Request $request
     * @return string|void
     * @url http://localhost/news/5.html?name=jiang
     */
    public function index(Request $request)
    {

       $this->assign("key",'vaule');
       $this->view->key2='value2';
       View::share("key3","vaule3");

       return $this->fetch('index',['email'=>'432433434@gmail.com','user'=>'jiang',],['STATI'=>"替換內容"]);
    }

}

二.變量替換可以在config 設置也可以用fetch第三個參數

 // 視圖輸出字符串內容替換
    'view_replace_str' => [’_123_'=>'替換內容'],

三. thinkphp褲架 \think\View.php提供了幾個全家路徑變量

   $baseReplace = [
            '__ROOT__'   => $root,
            '__URL__'    => $base . '/' . $request->module() . '/' . Loader::parseName($request->controller()),
            '__STATIC__' => $root . '/static',
            '__CSS__'    => $root . '/static/css',
            '__JS__'     => $root . '/static/js',
        ];

這幾個變量可以直接在html中輸出

<body>
__URL__   #/index/index<BR/>
__STATIC__#/static<BR/>
__JS__  #/static/js<BR/>
__CSS__ #/static/css<BR/>

__ROOT__<BR/>
</body>
</html>

如要修改或添加可以在config文件的view_replace_str中修改

 // 視圖輸出字符串內容替換
    'view_replace_str' => [’_123_'=>'替換內容','__UPLOAD__'=>'/static/upload],

 

config文件

    'template' => [
        // 默認模板渲染規則 1 解析為小寫+下划線 2 全部轉換小寫
        'auto_rule' => 1,
        // 模板引擎類型 支持 php think 支持擴展
        'type' => 'Think',
        // 視圖基礎目錄,配置目錄為所有模塊的視圖起始目錄
        'view_base' => '',
        // 當前模板的視圖目錄 留空為自動獲取
        'view_path' => '',
        // 模板后綴
        'view_suffix' => 'html',
        // 模板文件名分隔符
        'view_depr' => DS,
        // 模板引擎普通標簽開始標記
        'tpl_begin' => '{',
        // 模板引擎普通標簽結束標記
        'tpl_end' => '}',
        // 標簽庫標簽開始標記
        'taglib_begin' => '{',
        // 標簽庫標簽結束標記
        'taglib_end' => '}',
    ],

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM