From bb3d16d273bb3e4552e4869dd22cb2c2d81f5387 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 26 Aug 2018 02:54:52 +0200 Subject: Added Twig template renderer, closes #338 --- src/Renderer/TwigEngine.php | 41 ++++++++++++++++++++++++++++++++++++ src/Renderer/TwigLoader.php | 26 +++++++++++++++++++++++ src/Renderer/TwigServiceProvider.php | 31 +++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/Renderer/TwigEngine.php create mode 100644 src/Renderer/TwigLoader.php create mode 100644 src/Renderer/TwigServiceProvider.php (limited to 'src/Renderer') diff --git a/src/Renderer/TwigEngine.php b/src/Renderer/TwigEngine.php new file mode 100644 index 00000000..55a2e299 --- /dev/null +++ b/src/Renderer/TwigEngine.php @@ -0,0 +1,41 @@ +twig = $twig; + } + + /** + * Render a twig template + * + * @param string $path + * @param array $data + * @return string + * @throws LoaderError|RuntimeError|SyntaxError + */ + public function get($path, $data = []) + { + return $this->twig->render($path, $data); + } + + /** + * @param string $path + * @return bool + */ + public function canRender($path) + { + return $this->twig->getLoader()->exists($path); + } +} diff --git a/src/Renderer/TwigLoader.php b/src/Renderer/TwigLoader.php new file mode 100644 index 00000000..154e6dbb --- /dev/null +++ b/src/Renderer/TwigLoader.php @@ -0,0 +1,26 @@ +registerTwigEngine(); + } + + protected function registerTwigEngine() + { + $viewsPath = $this->app->get('path.views'); + + $twigLoader = $this->app->make(TwigLoader::class, ['paths' => $viewsPath]); + $this->app->instance(TwigLoader::class, $twigLoader); + $this->app->instance(TwigLoaderInterface::class, $twigLoader); + + $twig = $this->app->make(Twig::class); + $this->app->instance(Twig::class, $twig); + + $twigEngine = $this->app->make(TwigEngine::class); + $this->app->instance('renderer.twigEngine', $twigEngine); + $this->app->tag('renderer.twigEngine', ['renderer.engine']); + } +} -- cgit v1.2.3-54-g00ecf