mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Added twig template engine for view. (#905)
This commit is contained in:
parent
05c375c07f
commit
8ecbae4947
@ -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
|
||||
|
||||
|
@ -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",
|
||||
|
@ -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` 接口。
|
||||
|
@ -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": {
|
||||
|
27
src/view/src/Engine/TwigEngine.php
Normal file
27
src/view/src/Engine/TwigEngine.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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">
|
||||
|
45
src/view/tests/TwigTest.php
Normal file
45
src/view/tests/TwigTest.php
Normal 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);
|
||||
}
|
||||
}
|
10
src/view/tests/tpl/index.twig
Normal file
10
src/view/tests/tpl/index.twig
Normal 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>
|
Loading…
Reference in New Issue
Block a user