視圖
webman默認使用的是php原生語法作為模版,在打開opcache
后具有最好的性能。除了php原生模版,webman還提供了Twig、 Blade、 think-template 模版引擎。
開啟opcache
使用視圖時,強烈建議開啟php.ini中opcache.enable
和opcache.enable_cli
兩個選項,以便模版引擎達到最好性能。
安裝Twig
1、composer安裝
composer require twig/twig
2、修改配置config/view.php
為
<?php
use support\view\Twig;
return [
'handler' => Twig::class
];
提示
其它配置選項通過options傳入,例如
return [
'handler' => Twig::class,
'options' => [
'debug' => false,
'charset' => 'utf-8'
]
];
安裝Blade
1、composer安裝
composer require psr/container ^1.1.1 webman/blade
2、修改配置config/view.php
為
<?php
use support\view\Blade;
return [
'handler' => Blade::class
];
安裝think-template
1、composer安裝
composer require topthink/think-template
2、修改配置config/view.php
為
<?php
use support\view\ThinkPHP;
return [
'handler' => ThinkPHP::class,
];
提示
其它配置選項通過options傳入,例如
return [
'handler' => ThinkPHP::class,
'options' => [
'view_suffix' => 'html',
'tpl_begin' => '{',
'tpl_end' => '}'
]
];
原生PHP模版引擎例子
創(chuàng)建文件 app/controller/UserController.php
如下
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
return view('user/hello', ['name' => 'webman']);
}
}
新建文件 app/view/user/hello.html
如下
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
hello <?=htmlspecialchars($name)?>
</body>
</html>
Twig模版引擎例子
修改配置config/view.php
為
<?php
use support\view\Twig;
return [
'handler' => Twig::class
];
app/controller/UserController.php
如下
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
return view('user/hello', ['name' => 'webman']);
}
}
文件 app/view/user/hello.html
如下
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
hello {{name}}
</body>
</html>
更多文檔參考 Twig
Blade 模版的例子
修改配置config/view.php
為
<?php
use support\view\Blade;
return [
'handler' => Blade::class
];
app/controller/UserController.php
如下
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
return view('user/hello', ['name' => 'webman']);
}
}
文件 app/view/user/hello.blade.php
如下
注意blade模版后綴名為
.blade.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
hello {{$name}}
</body>
</html>
更多文檔參考 Blade
ThinkPHP 模版的例子
修改配置config/view.php
為
<?php
use support\view\ThinkPHP;
return [
'handler' => ThinkPHP::class
];
app/controller/UserController.php
如下
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
return view('user/hello', ['name' => 'webman']);
}
}
文件 app/view/user/hello.html
如下
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
hello {$name}
</body>
</html>
更多文檔參考 think-template
模版賦值
除了使用view(模版, 變量數(shù)組)
給模版賦值,我們還可以在任意位置通過調用View::assign()
給模版賦值。例如:
<?php
namespace app\controller;
use support\Request;
use support\View;
class UserController
{
public function hello(Request $request)
{
View::assign([
'name1' => 'value1',
'name2'=> 'value2',
]);
View::assign('name3', 'value3');
return view('user/test', ['name' => 'webman']);
}
}
View::assign()
在某些場景下非常有用,例如某系統(tǒng)每個頁面首部都要顯示當前登錄者信息,如果每個頁面都將此信息通過 view('模版', ['user_info' => '用戶信息']);
賦值將非常麻煩。解決辦法就是在中間件中獲得用戶信息,然后通過View::assign()
將用戶信息賦值給模版。
關于視圖文件路徑
控制器
當控制器調用view('模版名',[]);
時,視圖文件按照如下規(guī)則查找:
/
開頭則直接使用該路徑查找視圖文件- 不是以
/
開頭并且非多應用時,使用app/view/
下對應的視圖文件 - 不是以
/
開頭并且是多應用時,使用app/應用名/view/
下對應的視圖文件 - 如果不傳模板參數(shù),自動根據(jù)2 3規(guī)則查找模板文件
例子:
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
// 等價于 return view('user/hello', ['name' => 'webman']);
// 等價于 return view('/app/view/user/hello', ['name' => 'webman']);
return view(['name' => 'webman']);
}
}
閉包函數(shù)
閉包函數(shù)$request->app
為空,不屬于任何應用,所以閉包函數(shù)使用app/view/
下的視圖文件,例如 config/route.php
里定義路由
Route::any('/admin/user/get', function (Reqeust $reqeust) {
return view('user', []);
});
會使用app/view/user.html
作為模版文件(當使用blade模版時模版文件為app/view/user.blade.php
)。
指定應用
為了多應用模式下模版可以復用,view($template, $data, $app = null) 提供了第三個參數(shù) $app
,可以用來指定使用哪個應用目錄下的模版。例如 view('user', [], 'admin');
會強制使用 app/admin/view/
下的視圖文件。
省略模板參數(shù)
在類的控制器里可以省略模板參數(shù),例如
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
// 等價于 return view('user/hello', ['name' => 'webman']);
// 等價于 return view('/app/view/user/hello', ['name' => 'webman']);
return view(['name' => 'webman']);
}
}
擴展twig
我們可以通過給配置view.extension
回調,來擴展twig視圖實例,例如config/view.php
如下
<?php
use support\view\Twig;
return [
'handler' => Twig::class,
'extension' => function (\Twig\Environment $twig) {
$twig->addExtension(new your\namespace\YourExtension()); // 增加Extension
$twig->addFilter(new \Twig\TwigFilter('rot13', 'str_rot13')); // 增加Filter
$twig->addFunction(new \Twig\TwigFunction('function_name', function () {})); // 增加函數(shù)
}
];
擴展blade
同樣的我們可以通過給配置view.extension
回調,來擴展blade視圖實例,例如config/view.php
如下
<?php
use support\view\Blade;
return [
'handler' => Blade::class,
'extension' => function (Jenssegers\Blade\Blade $blade) {
// 給blade添加指令
$blade->directive('mydate', function ($timestamp) {
return "<?php echo date('Y-m-d H:i:s', $timestamp); ?>";
});
}
];
blade使用component組件
假設需要添加一個Alert組件
新建 app/view/components/Alert.php
<?php
namespace app\view\components;
use Illuminate\View\Component;
class Alert extends Component
{
public function __construct()
{
}
public function render()
{
return view('components/alert')->rawBody();
}
}
新建 app/view/components/alert.blade.php
<div>
<b style="color: red">hello blade component</b>
</div>
/config/view.php
類似如下代碼
<?php
use support\view\Blade;
return [
'handler' => Blade::class,
'extension' => function (Jenssegers\Blade\Blade $blade) {
$blade->component('alert', app\view\components\Alert::class);
}
];
至此,Blade組件Alert設置完畢,模版里使用時類似如下
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
<x-alert/>
</body>
</html>
擴展think-template
think-template 使用view.options.taglib_pre_load
來擴展標簽庫,例如
<?php
use support\view\ThinkPHP;
return [
'handler' => ThinkPHP::class,
'options' => [
'taglib_pre_load' => your\namspace\Taglib::class,
]
];
詳情參考 think-template標簽擴展