americanreading / view-twig
Twig Implementation of Views
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: 2025-03-06 02:47:38 UTC
README
Twig-backed implementation of View
and ViewFactory
.
To use, include with Composer:
{
"require": {
"americanreading/view-twig": "^1",
"twig/twig": "^2"
}
}
Configure a Twig_Environment
and pass this to the TwigView
constructor along with the root path to the view templates.
$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);
Use the factory to provide View
instances for a given template file:
$view = $factory->getView('my-template.twig.html');
Render the view to a string, merging in a context array.
$html = $view->render(['dog' => 'Bear']);