/** * Loads the Twig instance and registers the autoloader. */ public function configure() { parent::configure(); $this->configuration = $this->context->getConfiguration(); // empty array becuase it changes based on the rendering context $this->loader = new Twig_Loader_Filesystem(array()); $this->twig = new sfTwigEnvironment($this->loader, array('cache' => sfConfig::get('sf_template_cache_dir'), 'debug' => sfConfig::get('sf_debug', false), 'sf_context' => $this->context)); if ($this->twig->isDebug()) { $this->twig->enableAutoReload(); $this->twig->setCache(null); } $this->loadExtensions(); }
/** * Twig adapter constructor. * * @param AbstractRenderer $renderer */ public function __construct(AbstractRenderer $renderer, array $config = []) { parent::__construct($renderer, $config); $this->loader = new TwigLoaderFilesystem([]); $application = $this->renderer->getApplication(); $this->twig = new \Twig_Environment($this->loader, $config); if ($application->isDebugMode() || isset($config['debug']) && true === $config['debug']) { $this->twig->enableDebug(); $this->twig->addExtension(new \Twig_Extension_Debug()); } elseif ($application->isClientSAPI()) { $this->twig->enableAutoReload(); $this->setTwigCache($application->getCacheDir() . DIRECTORY_SEPARATOR . 'twig'); } }
/** * Loads the Twig instance and registers the autoloader. */ public function configure() { parent::configure(); $this->configuration = $this->context->getConfiguration(); require_once sfConfig::get('sf_twig_lib_dir', dirname(__FILE__) . '/../lib/vendor/Twig/lib') . '/Twig/Autoloader.php'; Twig_Autoloader::register(); // empty array becuase it changes based on the rendering context $this->loader = new Twig_Loader_Filesystem(array()); $this->twig = new sfTwigEnvironment($this->loader, array('cache' => sfConfig::get('sf_template_cache_dir'), 'debug' => sfConfig::get('sf_debug', false), 'sf_context' => $this->context)); if ($this->twig->isDebug()) { $this->twig->enableAutoReload(); $this->twig->setCache(null); } $this->loadExtensions(); }
/** * Twig adapter constructor. * * @param AbstractRenderer $renderer */ public function __construct(AbstractRenderer $renderer, array $config = []) { parent::__construct($renderer, $config); $this->loader = new TwigLoaderFilesystem([]); $this->twig = new \Twig_Environment($this->loader, $config); $app = $this->renderer->getApplication(); if ($app->isDebugMode() || isset($config['debug']) && true === $config['debug']) { $this->twig->enableDebug(); $this->twig->addExtension(new \Twig_Extension_Debug()); } if (isset($config['enable_autoreload']) && true === $config['enable_autoreload']) { $this->twig->enableAutoReload(); } if (isset($config['enable_cache']) && true === $config['enable_cache']) { $cacheDir = isset($config['cache_dir']) && is_string($config['cache_dir']) ? $config['cache_dir'] : $app->getCacheDir() . DIRECTORY_SEPARATOR . 'twig'; $this->setTwigCache($cacheDir); } }
function dwInitTwigEnvironment(Twig_Environment $twig) { $twig->setCache(ROOT_PATH . '/tmp/twig'); $twig->enableAutoReload(); $twig->addExtension(new Twig_I18n_Extension()); $twig->addFilter(new Twig_SimpleFilter('purify', function ($dirty) { return dwGetHTMLPurifier()->purify($dirty); })); $twig->addFilter(new Twig_SimpleFilter('json', function ($arr) { $mask = 0; if (!empty($opts)) { if (!empty($opts['pretty'])) { $mask = $mask | JSON_PRETTY_PRINT; } } return json_encode($arr, $mask); })); $twig->addFilter(new Twig_SimpleFilter('css', function ($arr) { $css = ''; foreach ($arr as $prop => $val) { $css .= $prop . ':' . $val . ';'; } return $css; })); $twig->addFunction(new Twig_SimpleFunction('hook', function () { call_user_func_array(array(DatawrapperHooks::getInstance(), 'execute'), func_get_args()); })); $twig->addFunction(new Twig_SimpleFunction('has_hook', function ($hook) { return DatawrapperHooks::getInstance()->hookRegistered($hook); })); $twig->addFunction(new Twig_SimpleFunction('has_plugin', function ($plugin) { return DatawrapperPluginManager::loaded($plugin); })); $twig->addFilter(new Twig_SimpleFilter('lettering', function ($text) { $out = ''; foreach (str_split($text) as $i => $char) { $out .= '<span class="char' . $i . '">' . $char . '</span>'; } return $out; }, array('is_safe' => array('html')))); $loc = DatawrapperSession::getLanguage(); if ($loc == 'en') { $loc = 'en-US'; } \Moment\Moment::setLocale(str_replace('-', '_', $loc)); $twig->addFilter(new Twig_SimpleFilter('reltime', function ($time) { // return $time; return (new \Moment\Moment($time))->fromNow()->getRelative(); })); if (!empty($GLOBALS['dw_config']['debug'])) { $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump')); } return $twig; }
function dwInitTwigEnvironment(Twig_Environment $twig) { $twig->setCache(ROOT_PATH . '/tmp/twig'); $twig->enableAutoReload(); $twig->addExtension(new Twig_I18n_Extension()); $twig->addFilter(new Twig_SimpleFilter('purify', function ($dirty) { return dwGetHTMLPurifier()->purify($dirty); })); $twig->addFilter(new Twig_SimpleFilter('json', function ($arr) { $mask = 0; if (!empty($opts)) { if (!empty($opts['pretty'])) { $mask = $mask | JSON_PRETTY_PRINT; } } return json_encode($arr, $mask); })); $twig->addFilter(new Twig_SimpleFilter('css', function ($arr) { $css = ''; foreach ($arr as $prop => $val) { $css .= $prop . ':' . $val . ';'; } return $css; })); $twig->addFunction(new Twig_SimpleFunction('hook', function () { call_user_func_array(array(DatawrapperHooks::getInstance(), 'execute'), func_get_args()); })); $twig->addFunction(new Twig_SimpleFunction('has_hook', function ($hook) { return DatawrapperHooks::getInstance()->hookRegistered($hook); })); $twig->addFunction(new Twig_SimpleFunction('has_plugin', function ($plugin) { return DatawrapperPluginManager::loaded($plugin); })); $twig->addFilter(new Twig_SimpleFilter('lettering', function ($text) { $out = ''; foreach (str_split($text) as $i => $char) { $out .= '<span class="char' . $i . '">' . $char . '</span>'; } return $out; }, array('is_safe' => array('html')))); return $twig; }
/** * @param \Twig_Environment $twig * @return \Twig_Environment */ public function timber_loader_twig(\Twig_Environment $twig) { $twig->enableAutoReload(); return $twig; }
/** * Tries to add any custom extensions that have been defined in the template or the transformation's configuration. * * This method will read the `twig-extension` parameter of the transformation (which inherits the template's * parameter set) and try to add those extensions to the environment. * * @param Transformation $transformation * @param ProjectDescriptor $project * @param \Twig_Environment $twigEnvironment * * @throws \InvalidArgumentException if a twig-extension should be loaded but it could not be found. * * @return void */ protected function addExtensionsFromTemplateConfiguration(Transformation $transformation, ProjectDescriptor $project, \Twig_Environment $twigEnvironment) { $isDebug = $transformation->getParameter('twig-debug') ? $transformation->getParameter('twig-debug')->getValue() : false; if ($isDebug == 'true') { $twigEnvironment->enableDebug(); $twigEnvironment->enableAutoReload(); $twigEnvironment->addExtension(new \Twig_Extension_Debug()); } /** @var Template\Parameter $extension */ foreach ($transformation->getParametersWithKey('twig-extension') as $extension) { $extensionValue = $extension->getValue(); if (!class_exists($extensionValue)) { throw new \InvalidArgumentException('Unknown twig extension: ' . $extensionValue); } // to support 'normal' Twig extensions we check the interface to determine what instantiation to do. $implementsInterface = in_array('phpDocumentor\\Plugin\\Twig\\ExtensionInterface', class_implements($extensionValue)); $twigEnvironment->addExtension($implementsInterface ? new $extensionValue($project, $transformation) : new $extensionValue()); } }
<?php date_default_timezone_set('Asia/Saigon'); require_once 'Twig-1.18.0/lib/Twig/Autoloader.php'; Twig_Autoloader::register(); $templateDir = realpath('./templates'); $loader = new Twig_Loader_Filesystem($templateDir); $twig = new Twig_Environment($loader, array('cache' => './cache', 'debug' => true, 'charset' => 'utf-8')); $function = new Twig_SimpleFunction('translate', function ($v) { return $v; $arr = array('Đặt xe taxi' => 'Taxi Booking'); return isset($arr[$v]) ? $arr[$v] : $v; }); $twig->addFunction($function); $twig->enableAutoReload(true);