视图

webman默认使用的是php原生语法作为模版,在打开opcache后具有最好的性能。除了php原生模版,webman还提供了TwigBladethink-template 模版引擎。

开启opcache

使用视图时,强烈建议开启php.ini中opcache.enableopcache.enable_cli 两个选项,以便模版引擎达到最好性能。

安装Twig

1、composer安装

composer require twig/twig

2、修改配置config/view.php

  1. <?php
  2. use support\view\Twig;
  3. return [
  4. 'handler' => Twig::class
  5. ];

提示 其它配置选项通过options传入,例如

  1. return [
  2. 'handler' => Twig::class,
  3. 'options' => [
  4. 'debug' => false,
  5. 'charset' => 'utf-8'
  6. ]
  7. ];

安装Blade

1、composer安装

  1. composer require psr/container ^1.1.1 jenssegers/blade:~1.4.0

2、修改配置config/view.php

  1. <?php
  2. use support\view\Blade;
  3. return [
  4. 'handler' => Blade::class
  5. ];

安装think-template

1、composer安装

composer require topthink/think-template

2、修改配置config/view.php

  1. <?php
  2. use support\view\ThinkPHP;
  3. return [
  4. 'handler' => ThinkPHP::class,
  5. ];

提示 其它配置选项通过options传入,例如

  1. return [
  2. 'handler' => ThinkPHP::class,
  3. 'options' => [
  4. 'view_suffix' => 'html',
  5. 'tpl_begin' => '{',
  6. 'tpl_end' => '}'
  7. ]
  8. ];

原生PHP模版引擎例子

创建文件 app/controller/User.php 如下

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. class User
  5. {
  6. public function hello(Request $request)
  7. {
  8. return view('user/hello', ['name' => 'webman']);
  9. }
  10. }

新建文件 app/view/user/hello.html 如下

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>webman</title>
  6. </head>
  7. <body>
  8. hello <?=htmlspecialchars($name)?>
  9. </body>
  10. </html>

Twig模版引擎例子

修改配置config/view.php

  1. <?php
  2. use support\view\Twig;
  3. return [
  4. 'handler' => Twig::class
  5. ];

app/controller/User.php 如下

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. class User
  5. {
  6. public function hello(Request $request)
  7. {
  8. return view('user/hello', ['name' => 'webman']);
  9. }
  10. }

文件 app/view/user/hello.html 如下

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>webman</title>
  6. </head>
  7. <body>
  8. hello {{name}}
  9. </body>
  10. </html>

更多文档参考 Twig

Blade 模版的例子

修改配置config/view.php

  1. <?php
  2. use support\view\Blade;
  3. return [
  4. 'handler' => Blade::class
  5. ];

app/controller/User.php 如下

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. class User
  5. {
  6. public function hello(Request $request)
  7. {
  8. return view('user/hello', ['name' => 'webman']);
  9. }
  10. }

文件 app/view/user/hello.blade.php 如下

注意blade模版后缀名为.blade.php

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>webman</title>
  6. </head>
  7. <body>
  8. hello {{$name}}
  9. </body>
  10. </html>

更多文档参考 Blade

ThinkPHP 模版的例子

修改配置config/view.php

  1. <?php
  2. use support\view\ThinkPHP;
  3. return [
  4. 'handler' => ThinkPHP::class
  5. ];

app/controller/User.php 如下

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. class User
  5. {
  6. public function hello(Request $request)
  7. {
  8. return view('user/hello', ['name' => 'webman']);
  9. }
  10. }

文件 app/view/user/hello.html 如下

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>webman</title>
  6. </head>
  7. <body>
  8. hello {$name}
  9. </body>
  10. </html>

更多文档参考 think-template

模版赋值

除了使用view(模版, 变量数组)给模版赋值,我们还可以在任意位置通过调用View::assign()给模版赋值。例如:

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. use support\View;
  5. class User
  6. {
  7. public function hello(Request $request)
  8. {
  9. View::assign([
  10. 'name1' => 'value1',
  11. 'name2'=> 'value2',
  12. ]);
  13. View::assign('name3', 'value3');
  14. return view('user/test', ['name' => 'webman']);
  15. }
  16. }

View::assign()在某些场景下非常有用,例如某系统每个页面首部都要显示当前登录者信息,如果每个页面都将此信息通过 view('模版', ['user_info' => '用户信息']); 赋值将非常麻烦。解决办法就是在中间件中获得用户信息,然后通过View::assign()将用户信息赋值给模版。

关于视图文件路径

控制器

当控制器调用view('模版名',[]);时,视图文件按照如下规则查找:

  1. 非多应用时,使用 app/view/ 下对应的视图文件
  2. 多应用时,使用 app/应用名/view/ 下对应的视图文件

总结来说就是如果 $request->app 为空,则使用 app/view/下的视图文件,否则使用 app/{$request->app}/view/ 下的视图文件。

闭包函数

闭包函数$request->app 为空,不属于任何应用,所以闭包函数使用app/view/下的视图文件,例如 config/route.php 里定义路由

  1. Route::any('/admin/user/get', function (Reqeust $reqeust) {
  2. return view('user', []);
  3. });

会使用app/view/user.html作为模版文件(当使用blade模版时模版文件为app/view/user.blade.php)。

指定应用

为了多应用模式下模版可以复用,view($template, $data, $app = null) 提供了第三个参数 $app,可以用来指定使用哪个应用目录下的模版。例如 view('user', [], 'admin'); 会强制使用 app/admin/view/ 下的视图文件。