本篇內容介紹了“如何輸出thinkphp5.1框架模板賦值與變量”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
namespace app\index\controller;
use think\Controller;
use think\facade\View;
class Demo extends Controller
{
public function test1()
{
//直接將內容輸出到頁面,不通過模板
$con = '<h4 >PHP</h4>';
return $this->dieplay($con);
return $this->view->display($con);
return View::display($con);//靜態代理
}
//使用視圖將數據進行輸出:fetch()
public function test2()
{
//模板變量賦值:assign()
//1、普通變量
$this->view->assign('name','zhang');
$this->view->assign('age',23);
//批量賦值
$this->view->assign([
'sex' => '男',
'salary' => 1200
]);
//2、array
$this->view->assign('goods',[
'id' => 1,
'name' => '手機',
'model' => 'meta10',
'price' => 3000
]);
//3、object
$obj = new \stdClass();
$obj->course = 'PHP';
$obj->lecture = 'zhang';
$this->view->assign('info',$obj);
//4、const(系統常量)
define('SITE_NAME','PHP');
//在模板中輸出數據
//模板默認的目錄位于當前模塊的view目錄,模板文件默認位于以當前控制器命名的目錄中
return $this->view->fetch();
}
}創建視圖文件夾(application\index\view)
創建模板(application\index\view\demo\test2.html)
輸出變量:
{$name}<br>
{$age}<br>
{$sex}<br>
{$salary}<br>
<hr>
{//輸出數組}
{$goods.id}<br>
{$goods.name}<br>
{$goods['model']}<br>
{$goods['price']}<br>
<hr>
{//輸出對象}
{$info->course}<br>
{$info->lecture}<br>
<hr>
{//輸出常量}
{$Think.const.SITE_NAME}<br>
<hr>
{//輸出PHP系統常量}
{$Think.const.PHP_VERSION}<br>
{$Think.const.PHP_SO}<br>
<hr>
{//輸出系統變量}
{$Think.server.php_self}<br>
{$Think.server.session.id}<br>
{$Think.server.get.name}<br>
{$Think.server.post.name}<br>
<hr>
{//輸出數據庫配置}
{$Think.config.database.hostname}<br>
<hr>
{//輸出請求變量}
{$Request.get.name}<br>
{$Request.param.name}<br>
{$Request.path}<br>
{$Request.root}<br>
{$Request.root.true}<br>
{//查詢當前控制器}
{$Request.controller}<br>
{//查詢當前方法}
{$Request.action}<br>
{//查詢域名}
{$Request.host}<br>
{//查詢ip}
{$Request.ip}<br>“如何輸出thinkphp5.1框架模板賦值與變量”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。