protected function getExtensions() { // should be moved to the Form component once absolute file paths are supported // by the default name parser in the Templating component $reflClass = new \ReflectionClass('Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'); $root = realpath(dirname($reflClass->getFileName()) . '/Resources/views'); $rootTheme = realpath(__DIR__ . '/Resources'); $templateNameParser = new StubTemplateNameParser($root, $rootTheme); $loader = new FilesystemLoader(array()); $this->engine = new PhpEngine($templateNameParser, $loader); $this->engine->addGlobal('global', ''); $this->engine->setHelpers(array(new TranslatorHelper(new StubTranslator()))); return array_merge(parent::getExtensions(), array(new TemplatingExtension($this->engine, $this->csrfTokenManager, array('FrameworkBundle:Form')))); }
/** * @param type $template Le template à afficher * @param array Les options à transmettre à la vue pour les utiliser * @return Response */ protected function render($template, array $options = array()) { $loader = new FilesystemLoader(__DIR__ . '/../view/%name%'); $templating = new PhpEngine(new TemplateNameParser(), $loader); $templating->set(new SlotsHelper()); $templating->set(new AssetsHelper('/')); $templating->set(new RouterHelper(new UrlGenerator($this->routes, $this->requestContext))); $templating->addGlobal('request', $this->request); //Marche bien $templating->addGlobal('session', $this->request->getSession()); $templating->addGlobal('user', $this->request->getSession() ? $this->request->getSession()->get('user') : null); $response = Response::create($templating->render($template, $options)); return $response; }
protected function setUp() { parent::setUp(); $root = realpath(__DIR__ . '/../../../Resources/views'); $rootTheme = realpath(__DIR__ . '/Resources'); $templateNameParser = new StubTemplateNameParser($root, $rootTheme); $loader = new FilesystemLoader(array()); $engine = new PhpEngine($templateNameParser, $loader); $engine->addGlobal('global', ''); $this->helper = new FormHelper($engine, $this->getMock('Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface'), array('FrameworkBundle:Form', 'FrameworkBundle:FormTable')); $engine->setHelpers(array($this->helper, new TranslatorHelper(new StubTranslator()))); }
public function initPlugin() { $this->app[$this->getPluginName()] = function ($app) { $paths = $app->config('templating.paths') ?: [$app['dir.root'] . '/module/' . $app['app.module'] . '/template/%name%']; $basePath = $app->config('templating.basePath') ?: $app->request()->getBasePath() . '/assets'; $loader = new FilesystemLoader($paths); $engine = new PhpEngine(new TemplateNameParser(), $loader); $engine->addGlobal('app', $app); $engine->set(new SlotsHelper()); $engine->set(new AssetsHelper($basePath)); $app->event()->emit('plugin.templating.after', [$app, $engine]); return $engine; }; }
/** * Templating engines currently supported: * - PHP * - Twig * - Smarty * - Mustache. * * @param ServiceManager $serviceManager * * @throws \RuntimeException */ public function configureServiceManager(ServiceManager $serviceManager) { $config = $serviceManager->get('Config'); $appRootDir = $config['parameters']['app.root_dir']; $appCacheDir = $config['parameters']['app.cache_dir']; $appCharset = $config['parameters']['app.charset']; // The "framework.templating" option is deprecated. Please replace it with "framework.view" $config = $this->processConfiguration($config); // these are the templating engines currently supported // @todo - this needs to come from the app config. $knownEngineIds = array('php', 'smarty', 'twig', 'mustache', 'plates', 'latte'); // these are the engines selected by the user $engineIds = isset($config['engines']) ? $config['engines'] : array('php'); // filter templating engines $engineIds = array_intersect($engineIds, $knownEngineIds); if (empty($engineIds)) { throw new \RuntimeException(sprintf('At least one templating engine should be defined in your app config (in $config[\'view.engines\']). These are the available ones: "%s". Example: "$config[\'templating.engines\'] = array(\'%s\');"', implode('", ', $knownEngineIds), implode("', ", $knownEngineIds))); } /* * Templating Locator. */ $serviceManager->setFactory('templating.locator', function ($serviceManager) use($appCacheDir) { return new TemplateLocator($serviceManager->get('file_locator'), $appCacheDir); }); /* * Templating Name Parser. */ $serviceManager->setFactory('templating.name_parser', function ($serviceManager) { return new TemplateNameParser($serviceManager->get('modulemanager')); }); /* * Filesystem Loader. */ $serviceManager->setFactory('templating.loader.filesystem', function ($serviceManager) { return new FileSystemLoader($serviceManager->get('templating.locator')); }); /* * Templating assets helper. */ $serviceManager->setFactory('templating.helper.assets', function ($serviceManager) { return new AssetsHelper($serviceManager->get('request')->getBasePath()); }); /* * Templating globals. */ $serviceManager->setFactory('templating.globals', function ($serviceManager) { return new GlobalVariables($serviceManager->get('servicemanager')); }); /* * PHP Engine. * * TODO: Migrate to Symfony\Bundle\FrameworkBundle\Templating\PhpEngine */ $serviceManager->setFactory('templating.engine.php', function ($serviceManager) use($appCharset) { $engine = new PhpEngine($serviceManager->get('templating.name_parser'), $serviceManager->get('templating.loader'), array(new SlotsHelper(), $serviceManager->get('templating.helper.assets'), new RouterHelper($serviceManager->get('router')), new SessionHelper($serviceManager->get('session')))); $engine->addGlobal('app', $serviceManager->get('templating.globals')); $engine->setCharset($appCharset); return $engine; }); /* * Twig Engine */ $serviceManager->setFactory('templating.engine.twig', function ($serviceManager) { if (!class_exists('Twig_Environment')) { throw new \Exception('PPI\\Framework\\TwigModule not found. Composer require: ppi/twig-module'); } $twigEnvironment = new \Twig_Environment(new \PPI\Framework\View\Twig\Loader\FileSystemLoader($serviceManager->get('templating.locator'), $serviceManager->get('templating.name_parser'))); // Add some twig extension $twigEnvironment->addExtension(new \PPI\Framework\View\Twig\Extension\AssetsExtension($serviceManager->get('templating.helper.assets'))); $twigEnvironment->addExtension(new \PPI\Framework\View\Twig\Extension\RouterExtension($serviceManager->get('router'))); return new \PPI\Framework\View\Twig\TwigEngine($twigEnvironment, $serviceManager->get('templating.name_parser'), $serviceManager->get('templating.locator'), $serviceManager->get('templating.globals')); }); /* * Smarty Engine. */ $serviceManager->setFactory('templating.engine.smarty', function ($serviceManager) use($appCacheDir) { if (!class_exists('NoiseLabs\\Bundle\\SmartyBundle\\SmartyEngine')) { throw new \Exception('PPI\\Framework\\SmartyModule not found. Composer require: ppi/smarty-module'); } $cacheDir = $appCacheDir . DIRECTORY_SEPARATOR . 'smarty'; $smartyEngine = new \PPI\Framework\View\Smarty\SmartyEngine(new \Smarty(), $serviceManager->get('templating.locator'), $serviceManager->get('templating.name_parser'), $serviceManager->get('templating.loader'), array('cache_dir' => $cacheDir . DIRECTORY_SEPARATOR . 'cache', 'compile_dir' => $cacheDir . DIRECTORY_SEPARATOR . 'templates_c'), $serviceManager->get('templating.globals'), $serviceManager->get('logger')); // Add some SmartyBundle extensions $smartyEngine->addExtension(new SmartyAssetsExtension($serviceManager->get('templating.helper.assets'))); $smartyEngine->addExtension(new SmartyRouterExtension($serviceManager->get('router'))); return $smartyEngine; }); // Mustache Engine $serviceManager->setFactory('templating.engine.mustache', function ($serviceManager, $appCacheDir) { if (!class_exists('Mustache_Engine')) { throw new \Exception('PPI\\Framework\\MustacheModule not found. Composer require: ppi/mustache-module'); } $rawMustacheEngine = new \Mustache_Engine(array('loader' => new MustacheFileSystemLoader($serviceManager->get('templating.locator'), $serviceManager->get('templating.name_parser')), 'cache' => $appCacheDir . DIRECTORY_SEPARATOR . 'mustache')); return new MustacheEngine($rawMustacheEngine, $serviceManager->get('templating.name_parser')); }); /* * Delegating Engine. */ $serviceManager->setFactory('templating.engine.delegating', function ($serviceManager) use($engineIds) { $delegatingEngine = new DelegatingEngine(); // @todo - lazy load this foreach ($engineIds as $id) { $delegatingEngine->addEngine($serviceManager->get('templating.engine.' . $id)); } return $delegatingEngine; }); $serviceManager->setAlias('templating', 'templating.engine.delegating'); }