視圖
webman默認(rèn)使用的是php原生語(yǔ)法作為模版,在打開(kāi)opcache
后具有最好的性能。除了php原生模版,webman還提供了Twig、 Blade、 think-template 模版引擎。
開(kāi)啟opcache
使用視圖時(shí),強(qiáng)烈建議開(kāi)啟php.ini中opcache.enable
和opcache.enable_cli
兩個(gè)選項(xiàng),以便模版引擎達(dá)到最好性能。
安裝Twig
1、composer安裝
composer require twig/twig
2、修改配置config/view.php
為
<?php
use support\view\Twig;
return [
'handler' => Twig::class
];
提示
其它配置選項(xiàng)通過(guò)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,
];
提示
其它配置選項(xiàng)通過(guò)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ù)組)
給模版賦值,我們還可以在任意位置通過(guò)調(diào)用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()
在某些場(chǎng)景下非常有用,例如某系統(tǒng)每個(gè)頁(yè)面首部都要顯示當(dāng)前登錄者信息,如果每個(gè)頁(yè)面都將此信息通過(guò) view('模版', ['user_info' => '用戶信息']);
賦值將非常麻煩。解決辦法就是在中間件中獲得用戶信息,然后通過(guò)View::assign()
將用戶信息賦值給模版。
關(guān)于視圖文件路徑
控制器
當(dāng)控制器調(diào)用view('模版名',[]);
時(shí),視圖文件按照如下規(guī)則查找:
/
開(kāi)頭則直接使用該路徑查找視圖文件(此特性需要webman-framework>=1.6.0
)- 不是以
/
開(kāi)頭并且非多應(yīng)用時(shí),使用app/view/
下對(duì)應(yīng)的視圖文件 - 不是以
/
開(kāi)頭并且是多應(yīng)用時(shí),使用app/應(yīng)用名/view/
下對(duì)應(yīng)的視圖文件 - 如果不傳模板參數(shù),自動(dòng)根據(jù)2 3規(guī)則查找模板文件(
此特性需要webman-framework>=1.6.0
)
例子:
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
// 等價(jià)于 return view('user/hello', ['name' => 'webman']);
// 等價(jià)于 return view('/app/view/user/hello', ['name' => 'webman']);
return view(['name' => 'webman']);
}
}
閉包函數(shù)
閉包函數(shù)$request->app
為空,不屬于任何應(yīng)用,所以閉包函數(shù)使用app/view/
下的視圖文件,例如 config/route.php
里定義路由
Route::any('/admin/user/get', function (Reqeust $reqeust) {
return view('user', []);
});
會(huì)使用app/view/user.html
作為模版文件(當(dāng)使用blade模版時(shí)模版文件為app/view/user.blade.php
)。
指定應(yīng)用
為了多應(yīng)用模式下模版可以復(fù)用,view($template, $data, $app = null) 提供了第三個(gè)參數(shù) $app
,可以用來(lái)指定使用哪個(gè)應(yīng)用目錄下的模版。例如 view('user', [], 'admin');
會(huì)強(qiáng)制使用 app/admin/view/
下的視圖文件。
省略模板參數(shù)
注意
此特性需要webman-framework>=1.6.0
在類的控制器里可以省略模板參數(shù),例如
<?php
namespace app\controller;
use support\Request;
class UserController
{
public function hello(Request $request)
{
// 等價(jià)于 return view('user/hello', ['name' => 'webman']);
// 等價(jià)于 return view('/app/view/user/hello', ['name' => 'webman']);
return view(['name' => 'webman']);
}
}
擴(kuò)展twig
注意
此特性需要webman-framework>=1.4.8
我們可以通過(guò)給配置view.extension
回調(diào),來(lái)擴(kuò)展twig視圖實(shí)例,例如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ù)
}
];
擴(kuò)展blade
注意
此特性需要webman-framework>=1.4.8
同樣的我們可以通過(guò)給配置view.extension
回調(diào),來(lái)擴(kuò)展blade視圖實(shí)例,例如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組件
注意
需要webman/blade>=1.5.2
假設(shè)需要添加一個(gè)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設(shè)置完畢,模版里使用時(shí)類似如下
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>webman</title>
</head>
<body>
<x-alert/>
</body>
</html>
擴(kuò)展think-template
think-template 使用view.options.taglib_pre_load
來(lái)擴(kuò)展標(biāo)簽庫(kù),例如
<?php
use support\view\ThinkPHP;
return [
'handler' => ThinkPHP::class,
'options' => [
'taglib_pre_load' => your\namspace\Taglib::class,
]
];