americanreading / view-twig
v2.1.0
2021-11-05 19:35 UTC
Requires
- americanreading/view: ^1
- twig/twig: >=2
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-06 01:37:22 UTC
README
View
和 ViewFactory
的Twig后端实现。
使用时,通过Composer引入
{
"require": {
"americanreading/view-twig": "^1",
"twig/twig": "^2"
}
}
配置一个 Twig_Environment
并将其传递给 TwigView
构造函数,同时提供视图模板的根路径。
$templateRoot = '/path/to/twig/template/files';
$loader = new \Twig_Loader_Filesystem($templateRoot);
$conf = [
'debug' => true,
'cache' => false,
'autoescape' => false
];
$twig = new \Twig_Environment($loader, $conf);
// Provide a context array to use as a default for all views.
$defaultContext = [
'cat' => 'Molly'
];
// Create the factory.
$factory = new TwigViewFactory($twig, $templateRoot, $defaultContext);
使用工厂提供特定模板文件的 View
实例
$view = $factory->getView('my-template.twig.html');
将视图渲染为一个字符串,并合并上下文数组。
$html = $view->render(['dog' => 'Bear']);