Skip to content

Commit

Permalink
Added twig template engine for view. (hyperf#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dickens Gao authored and limingxinleo committed Nov 10, 2019
1 parent 05c375c commit 8ecbae4
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion doc/zh/view.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 引擎

Expand All @@ -78,6 +78,12 @@ composer require duncan3dc/blade
composer require smarty/smarty
```

### 安装 Twig 引擎

```bash
composer require twig/twig
```

### 接入其他模板

假设我们想要接入一个虚拟的模板引擎名为 `TemplateEngine`,那么我们需要在任意地方创建对应的 `TemplateEngine` 类,并实现 `Hyperf\View\Engine\EngineInterface` 接口。
Expand Down
1 change: 1 addition & 0 deletions src/view/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
27 changes: 27 additions & 0 deletions src/view/src/Engine/TwigEngine.php
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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);
}
}
2 changes: 1 addition & 1 deletion src/view/tests/SmartyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testRender()
];

$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">
Expand Down
45 changes: 45 additions & 0 deletions src/view/tests/TwigTest.php
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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);
}
}
File renamed without changes.
10 changes: 10 additions & 0 deletions src/view/tests/tpl/index.twig
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 8ecbae4

Please sign in to comment.