loadTemplate() public method

Loads a template by name.
public loadTemplate ( string $name ) : Twig_TemplateInterface
$name string The template name
return Twig_TemplateInterface A template instance representing the given template name
 /**
  * {@inheritdoc}
  */
 public function create($templateName, array $context = array())
 {
     if (!isset($this->templateConfigs[$templateName])) {
         throw new TemplateNotExistsException($templateName);
     }
     $templateConfig = $this->templateConfigs[$templateName];
     if (isset($context['utm'])) {
         $templateConfig['utm'] = array_merge($templateConfig['utm'], $context['utm']);
     }
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateConfig['template']);
     $subject = $template->renderBlock('subject', $context);
     $htmlBody = $template->renderBlock('body_html', $context);
     $textBody = $template->renderBlock('body_text', $context);
     if (empty($subject)) {
         throw new TemplatePartRequiredException('subject', $templateConfig['template']);
     }
     if (empty($htmlBody)) {
         throw new TemplatePartRequiredException('body_html', $templateConfig['template']);
     }
     $htmlBody = $this->cssToInline($htmlBody);
     $htmlBody = $this->addUtmParams($htmlBody, $templateConfig['host'], $templateConfig['utm']);
     if (empty($textBody) && $templateConfig['generate_text_version']) {
         $textBody = $this->htmlToText($htmlBody);
     }
     $message = \Swift_Message::newInstance()->setSubject($subject)->setBody($htmlBody, 'text/html');
     if (!empty($textBody)) {
         $message->addPart($textBody, 'text/plain');
     }
     return $message;
 }
Example #2
0
 /**
  * @param string $name
  * @param array  $context
  * @param callable|null $callback a callback to modify the mail before it is sent.
  */
 public function send($name, array $context = [], callable $callback = null)
 {
     $template = $this->twig->loadTemplate($name);
     $blocks = [];
     foreach (['from', 'from_name', 'to', 'to_name', 'subject', 'body_txt', 'body_html'] as $blockName) {
         $rendered = $this->renderBlock($template, $blockName, $context);
         if ($rendered) {
             $blocks[$blockName] = $rendered;
         }
     }
     $blocks = array_merge($context, $blocks);
     $mail = new \Swift_Message();
     $mail->setSubject($blocks['subject']);
     $mail->setFrom(isset($blocks['from_name']) ? [$blocks['from'] => $blocks['from_name']] : $blocks['from']);
     if (isset($blocks['to'])) {
         $mail->setTo(isset($blocks['to_name']) ? [$blocks['to'] => $blocks['to_name']] : $blocks['to']);
     }
     if (isset($blocks['body_txt']) && isset($blocks['body_html'])) {
         $mail->setBody($blocks['body_txt']);
         $mail->addPart($blocks['body_html'], 'text/html');
     } elseif (isset($blocks['body_txt'])) {
         $mail->setBody($blocks['body_txt']);
     } elseif (isset($blocks['body_html'])) {
         $mail->setBody($blocks['body_html'], 'text/html');
     }
     if ($callback) {
         $callback($mail);
     }
     $this->mailer->send($mail);
 }
Example #3
0
 /**
  * Load a template.
  * @param string $template
  * @param string $namespace
  */
 public function __construct($template, $namespace = null)
 {
     if (!self::$initialised) {
         self::init();
     }
     $template = $template . '.twig';
     if (!is_null($namespace)) {
         $template = '@' . $namespace . '/' . $template;
     }
     $this->templateName = $template;
     $this->template = self::$twig->loadTemplate($template);
     Event::trigger('Template.Loaded', $this);
     switch ($namespace) {
         case 'db':
             // Do nothing
             break;
         case 'admin':
             Event::trigger('Template.Loaded.Admin', $this);
             break;
         default:
             Event::trigger('PublicTemplateLoaded', $this);
             Event::trigger('Template.Loaded.Public', $this);
             break;
     }
 }
Example #4
0
 /**
  * Sends an email
  *
  * @param string $recipient
  * @param string $templatePath Path to the twig template
  * @param array $data
  * @param array $blindCopyRecipients Recipients to send bcc
  *
  * @return bool
  */
 public function sendMail($recipient, $templatePath, $data = array(), $blindCopyRecipients = array())
 {
     $tmplData = array_merge($data, array('footerTxt' => $this->templateFooterTxtPath, 'footerHtml' => $this->templateFooterHtmlPath));
     // Load template from twig.
     $template = $this->twig->loadTemplate($templatePath);
     // Merge twig globals so that they also are available in renderBlock.
     $tmplData = $this->twig->mergeGlobals($tmplData);
     // Get subject from block.
     $subject = $template->renderBlock('subject', $tmplData);
     $emailBodyText = $template->renderBlock('body_text', $tmplData);
     $emailBodyHtml = $template->renderBlock('body_html', $tmplData);
     /** @var \Swift_Message $message */
     $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($this->emailFrom)->setTo($recipient)->setBody($emailBodyText, 'text/plain')->addPart($emailBodyHtml, 'text/html');
     // add blind copy recipients
     foreach ($blindCopyRecipients as $bcc) {
         $message->addBcc($bcc);
     }
     $failedRecipients = array();
     $this->mailer->send($message, $failedRecipients);
     if (count($failedRecipients) > 0) {
         $this->writeLog('Could not send mail to the following recipients: ' . join(', ', $failedRecipients));
         return false;
     }
     return true;
 }
 /**
  * {@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();
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array(__DIR__ . '/../../Resources/views/Form')));
     $this->twig->addExtension(new CKEditorExtension($this->helper));
     $this->template = $this->twig->loadTemplate('ckeditor_widget.html.twig');
 }
 protected final function _getTemplateResource($template)
 {
     if (!isset($this->__templateResources[$template])) {
         $this->__templateResources[$template] = $this->environment->loadTemplate($template);
     }
     return $this->__templateResources[$template];
 }
 /**
  * @return \Twig_TemplateInterface $template
  */
 protected function getIconTemplate()
 {
     if ($this->iconTemplate === null) {
         $this->iconTemplate = $this->environment->loadTemplate('@MopaBootstrap/icons.html.twig');
     }
     return $this->iconTemplate;
 }
 /**
  * @param UserDataEvent $event
  */
 public function onRequestedPassword(UserDataEvent $event)
 {
     $user = $event->getUser();
     $token = $this->tokenGenerator->generateToken();
     $this->userManager->updateConfirmationTokenUser($user, $token);
     $message = \Swift_Message::newInstance()->setCharset('UTF-8')->setSubject($this->templating->loadTemplate($this->template)->renderBlock('subject', []))->setFrom($this->from)->setTo($user->getEmail())->setBody($this->templating->loadTemplate($this->template)->renderBlock('body', ['username' => $user->getUsername(), 'request_link' => $this->router->generate('reset_password', ['token' => $token], true)]));
     $this->mailer->send($message);
 }
 /**
  * @param FileParameter $Location
  * @param bool          $Reload
  *
  * @return IBridgeInterface
  */
 public function loadFile(FileParameter $Location, $Reload = false)
 {
     $this->Instance = new \Twig_Environment(new \Twig_Loader_Filesystem(array(dirname($Location->getFile()))), array('auto_reload' => $Reload, 'autoescape' => false, 'cache' => realpath(__DIR__ . '/TwigTemplate')));
     $this->Instance->addFilter(new \Twig_SimpleFilter('utf8_encode', 'utf8_encode'));
     $this->Instance->addFilter(new \Twig_SimpleFilter('utf8_decode', 'utf8_decode'));
     $this->Template = $this->Instance->loadTemplate(basename($Location->getFile()));
     return $this;
 }
Example #11
0
 /**
  * Gets the templates for a given profile.
  *
  * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
  *
  * @return array
  */
 public function getTemplates(Profile $profile)
 {
     $templates = $this->getNames($profile);
     foreach ($templates as $name => $template) {
         $templates[$name] = $this->twig->loadTemplate($template);
     }
     return $templates;
 }
 /**
  * @param array                 $twigContext The twig context
  * @param HasPagePartsInterface $page        The page
  * @param string                $contextName The pagepart context
  * @param array                 $parameters  Some extra parameters
  *
  * @return string
  */
 public function renderIndexablePageParts(array $twigContext, HasPagePartsInterface $page, $contextName = 'main', array $parameters = array())
 {
     $template = $this->environment->loadTemplate('KunstmaanNodeSearchBundle:PagePart:view.html.twig');
     $pageparts = $this->indexablePagePartsService->getIndexablePageParts($page, $contextName);
     $newTwigContext = array_merge($parameters, array('pageparts' => $pageparts));
     $newTwigContext = array_merge($newTwigContext, $twigContext);
     return $template->render($newTwigContext);
 }
Example #13
0
 /**
  * sendNotification.
  *
  * @param string $templateName
  * @param array  $context
  */
 private function sendNotification($templateName, array $context)
 {
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $message = $template->renderBlock('body', $context);
     $notification = new Notification($message);
     $this->notifier->notify($notification);
 }
Example #14
0
 public function run()
 {
     $scanner = new ModuleScanner();
     $modules = $scanner->getModules();
     $template = $this->twig->loadTemplate("modules.twig");
     $output = $template->render(["modules" => $modules]);
     print $output;
 }
Example #15
0
 /**
  * {@inheritdoc}
  */
 public function send($template, array $recipients, array $data = array())
 {
     $data = $this->twig->mergeGlobals($data);
     $template = $this->twig->loadTemplate($template);
     $content = $template->renderBlock('content', $data);
     $originator = $template->renderBlock('originator', $data);
     $this->sender->send($recipients[0], $content, $originator);
 }
Example #16
0
 /**
  * @param string $template
  */
 public function setActionTemplate($template)
 {
     if ($this->showActionTemplate) {
         $this->actionTemplate = $this->templateEngine->loadTemplate($template);
     } else {
         unset($this->actionTemplate);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
 {
     if (null === $fieldDescription->getTemplate()) {
         $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
     }
     if (null === $fieldDescription->getType()) {
         $fieldDescription->setType('action');
     }
     if (null === $fieldDescription->getOption('name')) {
         $fieldDescription->setOption('name', 'Action');
     }
     if (null === $fieldDescription->getOption('code')) {
         $fieldDescription->setOption('code', 'Action');
     }
     if (null === $fieldDescription->getOption('header_style') && $fieldDescription->getOption('dropdown')) {
         $fieldDescription->setOption('header_style', 'width:40px');
     }
     if (null !== $fieldDescription->getOption('actions')) {
         $actions = $fieldDescription->getOption('actions');
         foreach ($actions as $k => &$action) {
             //only set the template if really exists
             //set to default any template that not exists
             if (!isset($action['template'])) {
                 if ($fieldDescription->getOption('dropdown')) {
                     $template = sprintf('SonataAdminBundle:CRUD:list__action_dropdown_%s.html.twig', $k);
                 } else {
                     $template = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k);
                 }
                 try {
                     $this->twig->loadTemplate($template);
                 } catch (\Twig_Error_Loader $e) {
                     if ($fieldDescription->getOption('dropdown')) {
                         $template = 'YnloAdminBundle::CRUD/list__action_dropdown_default.html.twig';
                     } else {
                         $template = 'YnloAdminBundle::CRUD/list__action_default.html.twig';
                     }
                 }
                 $action['template'] = $template;
             }
             //set default role
             if (!isset($action['role'])) {
                 $role = strtoupper($k);
                 $action['role'] = $role === 'SHOW' ? 'VIEW' : $role;
             }
             //set default visibility
             if (!isset($action['visible'])) {
                 $action['visible'] = true;
             }
         }
         $fieldDescription->setOption('actions', $actions);
     }
     //hide default label
     if (in_array($fieldDescription->getOption('label'), ['_action', 'Action'])) {
         $fieldDescription->setOption('label', ' ');
     }
     return $fieldDescription;
 }
 /**
  * Returns whether or not the given template exists.
  *
  * @param string $template
  *
  * @return bool
  */
 public function exists($template)
 {
     try {
         $this->twig->loadTemplate($template);
     } catch (\Exception $e) {
         return false;
     }
     return true;
 }
Example #19
0
 /**
  * Sends the email message.
  *
  * @param  string $templateName The template name
  * @param  array  $context      An array of context to pass to the template
  * @param  mixed $fromEmail     The from email
  * @param  mixed $toEmail       The to email
  */
 protected function sendMessage($templateName, array $context, $fromEmail, $toEmail)
 {
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $subject = $template->renderBlock('subject', $context);
     $body = $template->renderBlock('body', $context);
     $message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($fromEmail)->setTo($toEmail)->setBody($body, 'text/html');
     $this->mailer->send($message);
 }
Example #20
0
 /**
  * @param EmailInterface $email
  * @param array $data
  *
  * @return RenderedEmail
  */
 private function provideEmailWithTemplate(EmailInterface $email, array $data)
 {
     $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);
 }
 /**
  * @param array                    $twigContext The twig context
  * @param HasPageTemplateInterface $page        The page
  * @param array                    $parameters  Some extra parameters
  *
  * @return string
  */
 public function renderPageTemplate(array $twigContext, HasPageTemplateInterface $page, array $parameters = array())
 {
     $pageTemplateConfigurationReader = new PageTemplateConfigurationReader($this->kernel);
     $pageTemplates = $pageTemplateConfigurationReader->getPageTemplates($page);
     /* @var $pageTemplate PageTemplate */
     $pageTemplate = $pageTemplates[$this->getPageTemplate($page)];
     $template = $this->environment->loadTemplate($pageTemplate->getTemplate());
     return $template->render(array_merge($parameters, $twigContext));
 }
Example #22
0
 /**
  * {@inheritDoc}
  */
 public function canRender($template)
 {
     try {
         $this->twig->loadTemplate($template);
         return true;
     } catch (\Twig_Error_Loader $ex) {
         return false;
     }
 }
Example #23
0
 /**
  * Send the auth code to the user via email.
  *
  * @param TwoFactorInterface $user
  */
 public function sendAuthCode(TwoFactorInterface $user)
 {
     $template = $this->twig->loadTemplate('WallabagUserBundle:TwoFactor:email_auth_code.html.twig');
     $subject = $template->renderBlock('subject', []);
     $bodyHtml = $template->renderBlock('body_html', ['user' => $user->getName(), 'code' => $user->getEmailAuthCode(), 'support_url' => $this->supportUrl, 'wallabag_url' => $this->wallabagUrl]);
     $bodyText = $template->renderBlock('body_text', ['user' => $user->getName(), 'code' => $user->getEmailAuthCode(), 'support_url' => $this->supportUrl]);
     $message = new \Swift_Message();
     $message->setTo($user->getEmail())->setFrom($this->senderEmail, $this->senderName)->setSubject($subject)->setBody($bodyText, 'text/plain')->addPart($bodyHtml, 'text/html');
     $this->mailer->send($message);
 }
Example #24
0
 /**
  * sendNotification.
  *
  * @param string $templateName
  * @param array  $context
  * @param array  $options
  */
 private function sendNotification($templateName, array $context, array $options = [])
 {
     $optionsResolver = new OptionsResolver();
     $optionsResolver->setRequired(['color']);
     $options = $optionsResolver->resolve($options);
     $context = $this->twig->mergeGlobals($context);
     $template = $this->twig->loadTemplate($templateName);
     $message = $template->renderBlock('body', $context);
     $this->notifier->send($message, $options);
 }
 /**
  * Renders a menu with the specified renderer.
  *
  * @param \Knp\Menu\ItemInterface $item
  * @param array $options
  * @return string
  */
 public function render(ItemInterface $item, array $options = array())
 {
     $options = array_merge($this->defaultOptions, $options);
     $template = $options['template'];
     if (!$template instanceof \Twig_Template) {
         $template = $this->environment->loadTemplate($template);
     }
     $block = $options['compressed'] ? 'compressed_root' : 'root';
     return $template->renderBlock($block, array('item' => $item, 'options' => $options));
 }
 /**
  * @param array                 $twigContext The twig context
  * @param HasPagePartsInterface $page        The page
  * @param string                $contextName The pagepart context
  * @param array                 $parameters  Some extra parameters
  *
  * @return string
  */
 public function renderPageParts(array $twigContext, HasPagePartsInterface $page, $contextName = "main", array $parameters = array())
 {
     $template = $this->environment->loadTemplate("KunstmaanPagePartBundle:PagePartTwigExtension:widget.html.twig");
     /* @var $entityRepository PagePartRefRepository */
     $entityRepository = $this->em->getRepository('KunstmaanPagePartBundle:PagePartRef');
     $pageparts = $entityRepository->getPageParts($page, $contextName);
     $newTwigContext = array_merge($parameters, array('pageparts' => $pageparts));
     $newTwigContext = array_merge($newTwigContext, $twigContext);
     return $template->render($newTwigContext);
 }
 /**
  * @param FieldDescriptionInterface $fieldDescription
  * @param string                    $defaultTemplate
  *
  * @return \Twig_TemplateInterface
  */
 protected function getTemplate(FieldDescriptionInterface $fieldDescription, $defaultTemplate)
 {
     $templateName = $fieldDescription->getTemplate() ?: $defaultTemplate;
     try {
         $template = $this->environment->loadTemplate($templateName);
     } catch (\Twig_Error_Loader $e) {
         $template = $this->environment->loadTemplate($defaultTemplate);
     }
     return $template;
 }
 /**
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  * @param string $default
  * @return \Twig_TemplateInterface
  */
 protected function getTemplate(FieldDescriptionInterface $fieldDescription, $default)
 {
     // todo: find a better solution
     try {
         $template = $this->environment->loadTemplate($fieldDescription->getTemplate());
     } catch (\Twig_Error_Loader $e) {
         $template = $this->environment->loadTemplate($default);
     }
     return $template;
 }
 /**
  * Render locale switcher widget.
  *
  * @param string $localeSwitcher The locale switcher
  * @param string $route          The route
  * @param array  $parameters     The route parameters
  *
  * @return string
  */
 public function renderWidget($localeSwitcher, $route, array $parameters = array())
 {
     $template = $this->environment->loadTemplate("KunstmaanAdminBundle:LocaleSwitcherTwigExtension:widget.html.twig");
     $locales = array();
     $help = strtok($localeSwitcher, "|");
     while ($help !== false) {
         $locales[] = $help;
         $help = strtok("|");
     }
     return $template->render(array_merge($parameters, array('locales' => $locales, 'route' => $route)));
 }
Example #30
0
 /**
  * Renders a table
  *
  * @param TableView $table
  * @param array     $options
  *
  * @return string
  */
 public function render(TableView $table, array $options = [])
 {
     $options = array_merge($this->defaultOptions, $options);
     $template = $options['template'];
     if ($template instanceof \Twig_Template) {
         $this->template = $template;
     } else {
         $this->template = $this->environment->loadTemplate($template);
     }
     return $this->template->renderBlock('table', ['table' => $table, 'options' => $options]);
 }