Skip to content

Commit f4ebe41

Browse files
chenkbysy-records
andauthored
Added template_suffix configuration to twig engine. (#6402)
Co-authored-by: sy-records <52o@qq52o.cn>
1 parent b7f1329 commit f4ebe41

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/Engine/TwigEngine.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function render(string $template, array $data, array $config): string
2121
$loader = new FilesystemLoader($config['view_path']);
2222
$twig = new Environment($loader, ['cache' => $config['cache_path']]);
2323

24+
if ($suffix = $config['template_suffix'] ?? '') {
25+
$template .= $suffix;
26+
}
27+
2428
return $twig->render($template, $data);
2529
}
2630
}

tests/TwigTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ public function testRender()
4141
<body>
4242
Hello, Hyperf. You are using twig template now.
4343
</body>
44+
</html>', $res);
45+
}
46+
47+
public function testConfig()
48+
{
49+
$config = [
50+
'view_path' => __DIR__ . '/tpl',
51+
'cache_path' => __DIR__ . '/runtime',
52+
'template_suffix' => '.twig',
53+
];
54+
55+
$engine = new TwigEngine();
56+
$res = $engine->render('index', ['name' => 'Hyperf'], $config);
57+
58+
$this->assertEquals('<!DOCTYPE html>
59+
<html lang="en">
60+
<head>
61+
<meta charset="UTF-8">
62+
<title>Hyperf</title>
63+
</head>
64+
<body>
65+
Hello, Hyperf. You are using twig template now.
66+
</body>
4467
</html>', $res);
4568
}
4669
}

0 commit comments

Comments
 (0)