forked from hyperf/hyperf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added twig template engine for view. (hyperf#905)
- Loading branch information
1 parent
05c375c
commit 8ecbae4
Showing
9 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |