コード例 #1
0
ファイル: TemplatesService.php プロジェクト: andyra/tes
 /**
  * Returns the Twig Environment instance for a given template loader class.
  *
  * @param string $loaderClass The name of the class that should be initialized as the Twig instance’s template
  *                            loader. If no class is passed in, {@link TemplateLoader} will be used.
  *
  * @return TwigEnvironment The Twig Environment instance.
  */
 public function getTwig($loaderClass = null, $options = array())
 {
     if (!$loaderClass) {
         $loaderClass = __NAMESPACE__ . '\\TemplateLoader';
     }
     $options = array_merge(array('safe_mode' => false), $options);
     $cacheKey = $loaderClass . ':' . md5(serialize($options));
     if (!isset($this->_twigs[$cacheKey])) {
         $loader = new $loaderClass();
         $options = array_merge($this->_getTwigOptions(), $options);
         $twig = new TwigEnvironment($loader, $options);
         $twig->addExtension(new \Twig_Extension_StringLoader());
         $twig->addExtension(new CraftTwigExtension($twig));
         if (craft()->config->get('devMode')) {
             $twig->addExtension(new \Twig_Extension_Debug());
         }
         // Set our timezone
         $timezone = craft()->getTimeZone();
         $twig->getExtension('core')->setTimezone($timezone);
         // Set our custom parser to support "include" tags using the capture mode
         $twig->setParser(new TwigParser($twig));
         $this->_twigs[$cacheKey] = $twig;
         // Give plugins a chance to add their own Twig extensions
         $this->_addPluginTwigExtensions($twig);
     }
     return $this->_twigs[$cacheKey];
 }