Added twig template engine for view. (#905)

This commit is contained in:
Dickens Gao 2019-11-10 14:58:14 +08:00 committed by 李铭昕
parent 05c375c07f
commit 8ecbae4947
9 changed files with 94 additions and 3 deletions

View File

@ -3,6 +3,7 @@
## Added
- [#827](https://github.com/hyperf/hyperf/pull/827) Added a simple db component.
- [#905](https://github.com/hyperf/hyperf/pull/905) Added twig template engine for view.
## Fixed

View File

@ -58,7 +58,8 @@
"smarty/smarty": "^3.1",
"swoft/swoole-ide-helper": "dev-master",
"symfony/property-access": "^4.3",
"symfony/serializer": "^4.3"
"symfony/serializer": "^4.3",
"twig/twig": "^2.12"
},
"replace": {
"hyperf/amqp": "self.version",

View File

@ -64,7 +64,7 @@ return [
## 视图渲染引擎
官方目前支持 `Blade` `Smarty`种模板,默认安装 [hyperf/view](https://github.com/hyperf/view) 时不会自动安装任何模板引擎,需要您根据自身需求,自行安装对应的模板引擎,使用前必须安装任一模板引擎。
官方目前支持 `Blade` `Smarty` 和 `Twig`种模板,默认安装 [hyperf/view](https://github.com/hyperf/view) 时不会自动安装任何模板引擎,需要您根据自身需求,自行安装对应的模板引擎,使用前必须安装任一模板引擎。
### 安装 Blade 引擎
@ -78,6 +78,12 @@ composer require duncan3dc/blade
composer require smarty/smarty
```
### 安装 Twig 引擎
```bash
composer require twig/twig
```
### 接入其他模板
假设我们想要接入一个虚拟的模板引擎名为 `TemplateEngine`,那么我们需要在任意地方创建对应的 `TemplateEngine` 类,并实现 `Hyperf\View\Engine\EngineInterface` 接口。

View File

@ -26,6 +26,7 @@
"suggest": {
"hyperf/task": "Required to use task as a view render mode.",
"smarty/smarty": "Required to use smarty as a view render engine.",
"twig/twig": "Required to use twig as a view render engine.",
"duncan3dc/blade": "Required to use blade as a view render engine."
},
"autoload": {

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\View\Engine;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class TwigEngine implements EngineInterface
{
public function render($template, $data, $config): string
{
$loader = new FilesystemLoader($config['view_path']);
$twig = new Environment($loader, ['cache' => $config['cache_path']]);
return $twig->render($template, $data);
}
}

View File

@ -29,7 +29,7 @@ class SmartyTest extends TestCase
];
$engine = new SmartyEngine();
$res = $engine->render('smarty.tpl', ['name' => 'Hyperf'], $config);
$res = $engine->render('index.tpl', ['name' => 'Hyperf'], $config);
$this->assertEquals('<!DOCTYPE html>
<html lang="en">

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\View;
use Hyperf\View\Engine\TwigEngine;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class TwigTest extends TestCase
{
public function testRender()
{
$config = [
'view_path' => __DIR__ . '/tpl',
'cache_path' => __DIR__ . '/runtime',
];
$engine = new TwigEngine();
$res = $engine->render('index.twig', ['name' => 'Hyperf'], $config);
$this->assertEquals('<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperf</title>
</head>
<body>
Hello, Hyperf. You are using smarty template now.
</body>
</html>', $res);
}
}

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperf</title>
</head>
<body>
Hello, {{ name }}. You are using smarty template now.
</body>
</html>