/**
  * {@inheritdoc}
  */
 public function render(EmailInterface $email, array $data = array())
 {
     if (null !== $email->getTemplate()) {
         $data = $this->twig->mergeGlobals($data);
         /** @var \Twig_Template $template */
         $template = $this->twig->loadTemplate($email->getTemplate());
         if ($template->hasBlock('subject')) {
             $subject = $template->renderBlock('subject', $data);
         } else {
             $twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
             $subjectTemplate = $twig->createTemplate($email->getSubject());
             $subject = $subjectTemplate->render($data);
         }
         $body = $template->renderBlock('body', $data);
     } else {
         $twig = new \Twig_Environment(new \Twig_Loader_Array(array()));
         $subjectTemplate = $twig->createTemplate($email->getSubject());
         $bodyTemplate = $twig->createTemplate($email->getContent());
         $subject = $subjectTemplate->render($data);
         $body = $bodyTemplate->render($data);
     }
     /** @var EmailRenderEvent $event */
     $event = $this->dispatcher->dispatch(SyliusMailerEvents::EMAIL_PRE_RENDER, new EmailRenderEvent(new RenderedEmail($subject, $body)));
     return $event->getRenderedEmail();
 }
示例#2
0
 /**
  * @param EmailInterface $email
  * @param array $data
  *
  * @return RenderedEmail
  */
 private function provideEmailWithoutTemplate(EmailInterface $email, array $data)
 {
     $twig = new \Twig_Environment(new \Twig_Loader_Array([]));
     $subjectTemplate = $twig->createTemplate($email->getSubject());
     $bodyTemplate = $twig->createTemplate($email->getContent());
     $subject = $subjectTemplate->render($data);
     $body = $bodyTemplate->render($data);
     return new RenderedEmail($subject, $body);
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function __initialize(array $params)
 {
     /* @var \Twig_Template[] $template */
     static $template = array();
     $params['safe'] = false;
     parent::__initialize($params);
     $function = $this->getParam('label_function');
     if (!isset($template[$function])) {
         $template[$function] = $this->twig->createTemplate(sprintf('{{ %s(status) }}', $function));
     }
     $this->callback = function ($value) use($template, $function) {
         return $template[$function]->render(array('status' => $value));
     };
 }
示例#4
0
 public function testPagerWithReset()
 {
     $this->pagerfantaExtension->expects($this->once())->method('renderPagerfanta')->with($this->identicalTo($pager = $this->createPagerfantaMock()), $this->identicalTo($name = 'name'), $this->identicalTo($options = ['routeParams' => ['foo' => 'bar']]))->will($this->returnValue($result = '<div>result</div>'));
     $options['routeParams']['grid']['baz'] = 'bat';
     $options['routeParams']['grid']['reset'] = true;
     $this->assertSame($result, $this->twig->createTemplate('{{ lug_grid_pager(pager, name, options) }}')->render(['pager' => $pager, 'name' => $name, 'options' => $options]));
 }
示例#5
0
 public function doStuff(\Twig_Environment $twig, $file, $showCode = false, $wrapInIframe = false)
 {
     $node = self::$nodeRouter->findByPath($file);
     if ($node) {
         return $twig->createTemplate(self::$renderer->render($node, $showCode, $wrapInIframe))->render([]);
     }
     return '';
 }
示例#6
0
 /**
  * @param EmailInterface $email
  * @param array $data
  *
  * @return RenderedEmail
  */
 private function getRenderedEmail(EmailInterface $email, array $data)
 {
     if (null !== $email->getTemplate()) {
         $data = $this->twig->mergeGlobals($data);
         /** @var \Twig_Template $template */
         $template = $this->twig->loadTemplate($email->getTemplate());
         $subject = $template->renderBlock('subject', $data);
         $body = $template->renderBlock('body', $data);
         return new RenderedEmail($subject, $body);
     }
     $twig = new \Twig_Environment(new \Twig_Loader_Array([]));
     $subjectTemplate = $twig->createTemplate($email->getSubject());
     $bodyTemplate = $twig->createTemplate($email->getContent());
     $subject = $subjectTemplate->render($data);
     $body = $bodyTemplate->render($data);
     return new RenderedEmail($subject, $body);
 }
示例#7
0
文件: AppExtension.php 项目: itaw/cms
 public function renderPartial(\Twig_Environment $twig, $title, $parameters = [])
 {
     $partial = $this->doctrine->getRepository('DataBundle:Partial')->findOneByTitle($title);
     if (!$partial || !$partial->getActive()) {
         return;
     }
     $tmpl = $twig->createTemplate($partial->getContent());
     return $tmpl->render($parameters);
 }
 /**
  * @param \Twig_Environment $twig
  * @param string $alias
  * @return \Twig_Template
  * @throws \InvalidArgumentException
  */
 public function renderBlock(\Twig_Environment $twig, $alias)
 {
     $block = $this->repo->createQueryBuilder('b')->where('b.alias = :alias')->setParameters(compact('alias'))->setMaxResults(1)->getQuery()->useResultCache(true)->setResultCacheId('cms_block.' . $alias)->getResult();
     $block = current($block);
     if (!$block) {
         throw new \InvalidArgumentException(sprintf("CMS block '%s' could not be found", $alias));
     }
     return $twig->createTemplate($block->getContent());
 }
示例#9
0
 /**
  * {@inheritdoc}
  */
 public function __initialize(array $params)
 {
     /* @var \Twig_Template $template */
     static $template;
     $params['safe'] = false;
     parent::__initialize($params);
     $function = $this->getParam('label_function');
     if (!isset($template)) {
         $template = $this->twig->createTemplate(sprintf('<a href="{{ route }}">{{ client_name }}</a>', $function));
     }
     $this->callback = function ($clientName, Row $row, RouterInterface $router) use($template) {
         $clientId = $row->getField('client.id');
         if (!empty($clientId)) {
             $route = $router->generate('_clients_view', array('id' => $clientId));
             return $template->render(array('route' => $route, 'client_name' => $clientName));
         }
         return $clientName;
     };
 }
 /**
  * @dataProvider getEmbedFilterData
  */
 public function testEmbedFilter($template, $calls = 1)
 {
     $twig = new \Twig_Environment(new \Twig_Loader_Array(array()), array('debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0));
     $twig->addExtension(new CmfBlockExtension($this->getBlockHelper()));
     $this->getBlockHelper()->expects($this->exactly($calls))->method('embedBlocks');
     try {
         $twig->createTemplate($template)->render(array());
     } catch (\Twig_Error_Runtime $e) {
         throw $e->getPrevious();
     }
 }
 /**
  *
  * @param string $templateCode
  * @return \Twig_Template
  */
 public static function templateFromString($templateCode)
 {
     if (defined('TestMode')) {
         $options = array();
     } else {
         $options = array('cache' => APPPATH . 'cache');
     }
     $loader = new \Twig_Loader_Array(array());
     $twig = new \Twig_Environment($loader, $options);
     $template = $twig->createTemplate($templateCode);
     return $template;
 }
 /**
  * Render the template.
  *
  * @param string                        $template         The template string
  * @param LayoutInterface|MailInterface $templateInstance The template instance
  * @param array                         $variables        The variables of template
  *
  * @return string The rendered template
  *
  * @throws \Exception
  */
 protected function renderTemplate($template, $templateInstance, array $variables = array())
 {
     if (null !== $template) {
         if ($templateInstance instanceof TwigTemplateInterface) {
             $tpl = $this->renderer->loadTemplate($templateInstance->getFile());
             if ($tpl instanceof \Twig_Template) {
                 $template = $tpl->renderBlock($template, $variables);
                 $template = '' === $template ? null : $template;
             }
         } else {
             $tpl = $this->renderer->createTemplate($template);
             $template = $tpl->render($variables);
         }
     }
     return $template;
 }
示例#13
0
 /**
  * Renders content
  *
  * @param  Content $content
  * @return mixed
  */
 private function renderContent(Content $content)
 {
     $tpl = $content->has('template') ? " <comment>({$content->template})</comment>" : "";
     $this->app->writeln("Rendering: <info>{$content->target}</info>{$tpl}");
     // Only template files are run through Twig (template can be "none")
     if ($content->has('template')) {
         if ($content->paginate) {
             return $this->paginate($content);
         } else {
             $html = $this->twig->render($content->id, ['page' => $content, 'posts' => $this->getPosts($content), 'parent' => $this->getParent($content->parentId)]);
         }
     } else {
         $template = $this->twig->createTemplate($content->content);
         $html = $template->render([]);
     }
     // Save Content
     $this->savePage($content->target, $html);
 }
示例#14
0
 public function index(&$view, &$data, &$output)
 {
     if (!$this->config->get($this->config->get('config_theme') . '_status')) {
         exit('Error: A theme has not been assigned to this store!');
     }
     // This is only here for compatibility with older extensions
     if (substr($view, -3) == 'tpl') {
         $view = substr($view, 0, -3);
     }
     // If the default theme is selected we need to know which directory its pointing to
     if ($this->config->get('config_theme') == 'theme_default') {
         $theme = $this->config->get('theme_default_directory');
     } else {
         $theme = $this->config->get('config_theme');
     }
     // If there is a theme override we should get it
     $this->load->model('design/theme');
     $theme_info = $this->model_design_theme->getTheme($view, $theme);
     if ($theme_info) {
         // include and register Twig auto-loader
         include_once DIR_SYSTEM . 'library/template/Twig/Autoloader.php';
         Twig_Autoloader::register();
         // specify where to look for templates
         $loader = new \Twig_Loader_Filesystem(DIR_TEMPLATE);
         // initialize Twig environment
         $twig = new \Twig_Environment($loader, array('autoescape' => false));
         $template = $twig->createTemplate(html_entity_decode($theme_info['code'], ENT_QUOTES, 'UTF-8'));
         $output = $template->render($data);
     } else {
         if (is_file(DIR_TEMPLATE . $theme . '/template/' . $view . '.twig')) {
             $this->config->set('template_type', 'twig');
             $view = $theme . '/template/' . $view . '.twig';
         } elseif (is_file(DIR_TEMPLATE . 'default/template/' . $view . '.twig')) {
             $this->config->set('template_type', 'twig');
             $view = 'default/template/' . $view . '.twig';
         } elseif (is_file(DIR_TEMPLATE . $theme . '/template/' . $view . '.tpl')) {
             $this->config->set('template_type', 'php');
             $view = $theme . '/template/' . $view . '.tpl';
         } elseif (is_file(DIR_TEMPLATE . 'default/template/' . $view . '.tpl')) {
             $this->config->set('template_type', 'php');
             $view = 'default/template/' . $view . '.tpl';
         }
     }
 }
示例#15
0
/**
 * Loads a template from a string.
 *
 * <pre>
 * {{ include(template_from_string("Hello {{ name }}")) }}
 * </pre>
 *
 * @param Twig_Environment $env A Twig_Environment instance
 * @param string $template A template as a string
 *
 * @return Twig_Template A Twig_Template instance
 */
function twig_template_from_string(Twig_Environment $env, $template)
{
    return $env->createTemplate($template);
}
 /**
  * {@inheritdoc}
  */
 public function renderTemplateString(string $template, array $parameters = []) : string
 {
     $template = $this->environment->createTemplate($template);
     return $template->render($parameters);
 }
示例#17
0
 /**
  * @param $str
  * @param  array  $data
  * @return string
  */
 public function parse($str, array $data = array())
 {
     $template = $this->twig->createTemplate($str);
     return $template->render($data);
 }
示例#18
0
 /**
  * Process given license returning comment format.
  *
  * @param string $rawLicense raw license to process
  *
  * @throws \Exception
  * @throws \Throwable
  *
  * @return string parsed license
  */
 private function parseLicense($rawLicense)
 {
     $twig = new \Twig_Environment(new \Twig_Loader_Filesystem());
     $license = $twig->createTemplate($rawLicense)->render($this->config->getParameters());
     $license = trim($license);
     //create license comment
     $license = preg_replace("/\n/", "\n * ", $license);
     //clean empty lines, remove trailing whitespace
     $license = preg_replace("/ \\* \n/", " *\n", $license);
     //wrapper
     $license = "/*\n * {$license}\n */\n";
     return $license;
 }
 /**
  * @covers DataUri\TwigExtension::dataUri
  * @expectedException \Twig_Error_Runtime
  */
 public function testDataUriUnknownFormat()
 {
     $template = $this->twig->createTemplate('{{ array | dataUri(false) }}');
     $template->render(array('array' => array()));
 }
示例#20
0
 /**
  * Render the template and return the output.
  * @return string
  */
 public function render()
 {
     delete_transient($this->transient_notices);
     $loader = new \Twig_Loader_Filesystem(self::$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);
     $twig->addFilter(new \Twig_SimpleFilter('get_date_from_gmt', 'get_date_from_gmt'));
     // 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.
     if (!empty($this->template_string)) {
         $template = $twig->createTemplate($this->template_string);
     } else {
         $template = $twig->loadTemplate($this->template_name);
     }
     return $template->render($this->data);
 }
示例#21
0
 /**
  * @param string $source
  * @param array $context
  * @return string
  */
 protected function compileString($source, array $context)
 {
     return $this->twig->createTemplate($source)->render($context);
 }
 private function getRendered($template, $context = [])
 {
     $template = $this->twig->createTemplate($template);
     $content = $template->render($context);
     return $content;
 }
示例#23
0
 /**
  * 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);
 }