コード例 #1
0
ファイル: TwigFactory.php プロジェクト: kynx/zend-expressive
 /**
  * @param ContainerInterface $container
  * @return Twig
  */
 public function __invoke(ContainerInterface $container)
 {
     $config = $container->has('config') ? $container->get('config') : [];
     $debug = array_key_exists('debug', $config) ? (bool) $config['debug'] : false;
     $config = isset($config['templates']) ? $config['templates'] : [];
     $cacheDir = isset($config['cache_dir']) ? $config['cache_dir'] : false;
     // Create the engine instance
     $loader = new TwigLoader();
     $environment = new TwigEnvironment($loader, ['cache' => $debug ? false : $cacheDir, 'debug' => $debug, 'strict_variables' => $debug, 'auto_reload' => $debug]);
     // Add extensions
     if ($container->has(RouterInterface::class)) {
         $environment->addExtension(new Twig\TwigExtension($container->get(RouterInterface::class), isset($config['assets_url']) ? $config['assets_url'] : '', isset($config['assets_version']) ? $config['assets_version'] : ''));
     }
     if ($debug) {
         $environment->addExtension(new TwigExtensionDebug());
     }
     // Inject environment
     $twig = new Twig($environment, isset($config['extension']) ? $config['extension'] : 'html.twig');
     // Add template paths
     $allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : [];
     foreach ($allPaths as $namespace => $paths) {
         $namespace = is_numeric($namespace) ? null : $namespace;
         foreach ((array) $paths as $path) {
             $twig->addPath($path, $namespace);
         }
     }
     return $twig;
 }
コード例 #2
0
 /**
  * Add Twig
  */
 public function toHtml($content)
 {
     $twig = new Twig();
     $twig->addPath('views');
     return $twig->render('content.twig', ['content' => $content]);
     //return $content;
 }
コード例 #3
0
ファイル: TwigTest.php プロジェクト: pborreli/zend-expressive
 /**
  * @group namespacing
  */
 public function testResolvesNamespacedTemplateWithSuffix()
 {
     $template = new TwigTemplate();
     $template->addPath(__DIR__ . '/TestAsset/test', 'test');
     $expected = file_get_contents(__DIR__ . '/TestAsset/test/test.js');
     $test = $template->render('test::test.js');
     $this->assertSame($expected, $test);
 }
コード例 #4
0
 private function formatHtml($content)
 {
     $twig = new Twig();
     $twig->addPath('views');
     return $twig->render('content.twig', ['content' => $content]);
 }
コード例 #5
0
 /**
  * @dataProvider objectParameterValues
  */
 public function testCanRenderWithParameterObjects($params, $search)
 {
     $template = new TwigTemplate();
     $template->addPath(__DIR__ . '/TestAsset');
     $result = $template->render('twig.html', $params);
     $this->assertContains($search, $result);
     $content = file_get_contents(__DIR__ . '/TestAsset/twig.html');
     $content = str_replace('{{ name }}', $search, $content);
     $this->assertEquals($content, $result);
 }