Skip to content

Commit

Permalink
Added template_suffix configuration to twig engine. (#6402)
Browse files Browse the repository at this point in the history
Co-authored-by: sy-records <[email protected]>
  • Loading branch information
chenkby and sy-records authored Dec 21, 2023
1 parent b7f1329 commit f4ebe41
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Engine/TwigEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function render(string $template, array $data, array $config): string
$loader = new FilesystemLoader($config['view_path']);
$twig = new Environment($loader, ['cache' => $config['cache_path']]);

if ($suffix = $config['template_suffix'] ?? '') {
$template .= $suffix;
}

return $twig->render($template, $data);
}
}
23 changes: 23 additions & 0 deletions tests/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ public function testRender()
<body>
Hello, Hyperf. You are using twig template now.
</body>
</html>', $res);
}

public function testConfig()
{
$config = [
'view_path' => __DIR__ . '/tpl',
'cache_path' => __DIR__ . '/runtime',
'template_suffix' => '.twig',
];

$engine = new TwigEngine();
$res = $engine->render('index', ['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 twig template now.
</body>
</html>', $res);
}
}

0 comments on commit f4ebe41

Please sign in to comment.