/** * 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'); } }
/** * 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); } }
public function testOutput() { $this->fieldDescription->expects($this->any())->method('getTemplate')->will($this->returnValue('SonataAdminBundle:CRUD:base_list_field.html.twig')); $this->fieldDescription->expects($this->any())->method('getFieldName')->will($this->returnValue('fd_name')); $this->environment->disableDebug(); $parameters = array('admin' => $this->admin, 'value' => 'foo', 'field_description' => $this->fieldDescription, 'object' => $this->object); $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig'); $this->assertSame('<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>', trim(preg_replace('/\\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters)))); $this->environment->enableDebug(); $this->assertSame('<!-- START fieldName: fd_name template: SonataAdminBundle:CRUD:base_list_field.html.twig compiled template: SonataAdminBundle:CRUD:base_list_field.html.twig --> <td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td> <!-- END - fieldName: fd_name -->', trim(preg_replace('/\\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters)))); }
/** * Creates a Twig rendering engine * @param type $viewRootFolder * @param type $cacheFolder * @param type $debug * @return \Twig_Environment */ protected function createTwig($viewRootFolder, $cacheFolder, $debug) { $this->loader = new \Twig_Loader_Filesystem($viewRootFolder); $twig = new \Twig_Environment($this->loader); $twig->enableStrictVariables(); if ($debug === true) { $twig->enableDebug(); } else { $twig->disableDebug(); $twig->setCache($cacheFolder); } return $twig; }
/** * @param array $values */ public function __construct($values = array()) { $defaults['twig.options'] = array(); $defaults['twig.directories'] = array(); $defaults['twig.loader'] = function ($meadow) { // this needs to be lazy or theme switchers and alike explode it $stylesheet_dir = get_stylesheet_directory(); $template_dir = get_template_directory(); $calculated_dirs = array($stylesheet_dir, $template_dir, plugin_dir_path(__DIR__) . 'twig'); // enables explicit inheritance from parent theme in child if ($stylesheet_dir !== $template_dir) { $calculated_dirs[] = dirname($template_dir); } $directories = array_unique(array_merge($calculated_dirs, $meadow['twig.directories'])); return new \Twig_Loader_Filesystem($directories); }; $defaults['twig.undefined_function'] = array(__CLASS__, 'undefined_function'); $defaults['twig.undefined_filter'] = array(__CLASS__, 'undefined_filter'); $defaults['twig.environment'] = function ($meadow) { $environment = new \Twig_Environment($meadow['twig.loader'], $meadow['twig.options']); $meadow_extension = new Extension(); $environment->addExtension($meadow_extension); $environment->registerUndefinedFunctionCallback($meadow['twig.undefined_function']); $environment->registerUndefinedFilterCallback($meadow['twig.undefined_filter']); if (defined('WP_DEBUG') && WP_DEBUG) { $debug_extension = new \Twig_Extension_Debug(); $environment->addExtension($debug_extension); $environment->enableDebug(); } return $environment; }; $defaults['hierarchy'] = function () { return new Template_Hierarchy(); }; parent::__construct(array_merge($defaults, $values)); }
public function render() { delete_transient($this->transient_notices); $loader = new \Twig_Loader_Filesystem($this->paths); $twig = new \Twig_Environment($loader); // Add the admin_url() function. $twig->addFunction('admin_url', new \Twig_SimpleFunction('admin_url', 'admin_url')); // Add titlecase filter. $titlecase_filter = new \Twig_SimpleFilter('titlecase', '\\WordPress\\Tabulate\\Text::titlecase'); $twig->addFilter($titlecase_filter); // Add date and time filters. $date_filter = new \Twig_SimpleFilter('wp_date_format', '\\WordPress\\Tabulate\\Text::wp_date_format'); $twig->addFilter($date_filter); $time_filter = new \Twig_SimpleFilter('wp_time_format', '\\WordPress\\Tabulate\\Text::wp_time_format'); $twig->addFilter($time_filter); // Add strtolower filter. $strtolower_filter = new \Twig_SimpleFilter('strtolower', function ($str) { if (is_array($str)) { return array_map('strtolower', $str); } else { return strtolower($str); } }); $twig->addFilter($strtolower_filter); // Enable debugging. if (WP_DEBUG) { $twig->enableDebug(); $twig->addExtension(new \Twig_Extension_Debug()); } // Render the template. $template = $twig->loadTemplate($this->templateName); return $template->render($this->data); }
$counter = array('vehicles' => 0, 'realestate' => 0, 'jobs' => 0); //var_dump($template); //die('ok up to here'); //test mailer //echo send_email($generalConfig); $alertHandler = new Alert(); $formatter = new Formatter(); foreach ($regions as $region => $values) { $loader = new Twig_Loader_Filesystem(TWIG_TEMPLATES . '/' . $region); // TODO: // For some reason cache is not invalidated with chages - check it out then enable //$twig = new Twig_Environment($loader,array('cache' => TWIG_TEMPLATES.'/cache')); /// ... in the meantime $twig = new Twig_Environment($loader); if (SET_DEBUG) { $twig->enableDebug(); } else { $twig->disableDebug(); } // ... load the template outputStatus('Load html template ...'); $template = $twig->loadTemplate('send_recommended/send_all.html'); outputStatus('Region ' . $region); foreach ($sections as $section => $secParams) { outputStatus('Traverse section ' . $section); $formatter_conf = array('section' => $section, 'region' => $region, 'months' => $values['months'], 'base_domain' => $values['base_domain']); // var_dump($formatter_conf);die; $spx_config = $region . '_sphinx_' . $section; $alertHandler->resetArrays(); $liveDB = new DBAdapter2(array('host' => $conf->dbserver->host, 'username' => $conf->dbserver->username, 'password' => $conf->dbserver->password, 'schema' => $values['live_schema'])); // First drop all alerts older than specified!
/** * 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()); } }
/** * Render the template and return the output. * @return string */ public function render() { if (isset($_SESSION[$this->transientNotices])) { unset($_SESSION[$this->transientNotices]); } $twig = new \Twig_Environment($this->loader); // Add titlecase filter. $titlecase_filter = new \Twig_SimpleFilter('titlecase', '\\Tabulate\\Text::titlecase'); $twig->addFilter($titlecase_filter); // Add strtolower filter. $strtolower_filter = new \Twig_SimpleFilter('strtolower', function ($str) { if (is_array($str)) { return array_map('strtolower', $str); } else { return strtolower($str); } }); $twig->addFilter($strtolower_filter); // Enable debugging. if (Config::debug()) { $this->queries = DB\Database::getQueries(); $twig->enableDebug(); $twig->addExtension(new \Twig_Extension_Debug()); } // Render the template. if (!empty($this->templateString)) { $template = $twig->createTemplate($this->templateString); } else { $template = $twig->loadTemplate($this->templateName); } return $template->render($this->data); }