/**
  * Fills in message using blocks from a template (`body`, `subject`, `from`).
  * If there is no `body` block then whole template will be used.
  * If there is no `subject` or `from` block then corresponding default value will be used.
  * @param \Swift_Message $message
  * @param \Twig_Template $templateContent
  * @param array $data
  */
 protected function populateMessage(\Swift_Message $message, \Twig_Template $templateContent, $data)
 {
     $body = $templateContent->hasBlock('body') ? $templateContent->renderBlock('body', $data) : $templateContent->render($data);
     $subject = $templateContent->hasBlock('subject') ? $templateContent->renderBlock('subject', $data) : $this->defaultSubject;
     $from = $templateContent->hasBlock('from') ? $templateContent->renderBlock('from', $data) : $this->defaultFrom;
     $message->setFrom($from)->setSubject($subject)->setBody($body, 'text/html', 'utf-8');
 }
Esempio n. 2
1
 function it_prepares_email_properly($mailer, $twigEnvironment, \Twig_Template $template)
 {
     $from = '*****@*****.**';
     $to = '*****@*****.**';
     $templateName = 'test-template';
     $context = array('dummy', 'context');
     $template->renderBlock('subject', $context)->shouldBeCalled();
     $template->renderBlock('body_text', $context)->shouldBeCalled();
     $template->renderBlock('body_html', $context)->shouldBeCalled();
     $twigEnvironment->mergeGlobals($context)->shouldBeCalled()->willReturn($context);
     $twigEnvironment->loadTemplate($templateName)->willReturn($template);
     $mailer->send(Argument::any())->shouldBeCalled();
     $this->sendEmail($templateName, $context, $from, $to);
 }
 public function renderBlock(\Twig_Template $template, ColumnInterface $column, $object, $context, $blocks)
 {
     try {
         $value = $this->accessor->getValue($object, $column->getName());
     } catch (ExceptionInterface $exception) {
         $value = null;
     }
     $block = 'crudify_field_' . $column->getType();
     // check if the block exists
     $hasBlock = false;
     if (!isset($blocks[$block])) {
         $current = $template;
         do {
             if ($current->hasBlock($block)) {
                 break;
             }
             $current = $current->getParent($context);
         } while ($current instanceof \Twig_Template);
         if ($current instanceof \Twig_Template) {
             $hasBlock = true;
         }
     } else {
         $hasBlock = true;
     }
     if ($hasBlock) {
         $context['value'] = $value;
         $context['definition'] = $column->getParent()->getParent();
         $context['column'] = $column;
         $context['object'] = $object;
         $result = $template->renderBlock($block, $context, $blocks);
     } else {
         $result = htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8');
     }
     return $result;
 }
Esempio n. 4
1
 protected function renderTwigBlock(\Twig_Template $template, $blockName, $context = array())
 {
     foreach ($template->getEnvironment()->getGlobals() as $key => $value) {
         if (!array_key_exists($key, $context)) {
             $context[$key] = $value;
         }
     }
     return $template->renderBlock($blockName, $context);
 }
 /**
  * Render block.
  *
  * @param \Twig_Environment $twig
  * @param                   $name
  * @param                   $parameters
  *
  * @return string
  */
 private function renderBlock(\Twig_Environment $twig, $name, $parameters)
 {
     // load template if needed
     if (is_null($this->template)) {
         // get template name
         if (is_null($this->theme)) {
             $this->theme = 'PlatinumPixsSimplePaginationBundle::blocks.html.twig';
         }
         $this->template = $twig->loadTemplate($this->theme);
     }
     if ($this->template->hasBlock($name)) {
         return $this->template->renderBlock($name, $parameters);
     } else {
         throw new \InvalidArgumentException(sprintf('Block "%s" doesn\'t exist in template "%s".', $name, $this->theme));
     }
 }
 public function renderBlock(\Twig_Environment $env, $themeRoot, $name, array $variables)
 {
     if (!$this->template) {
         $this->template = $env->loadTemplate(reset($this->resources));
     }
     $blocks = $this->getBlocks($env, $themeRoot);
     return $this->template->renderBlock($name, $variables, $blocks);
 }
Esempio n. 7
0
 /**
  * Renders a cell
  *
  * @param Cell $cell
  *
  * @return string
  */
 public function renderCell(Cell $cell)
 {
     $block = $cell->vars['type'] . '_cell';
     /*if(!$this->template->hasBlock($block)) {
           $block = 'text_cell';
       }*/
     return trim($this->template->renderBlock($block, $cell->vars));
 }
 /**
  * {@inheritdoc}
  */
 public function sendMessage($text, UserInterface $user, \Twig_Template $twigTemplate, array $parameters)
 {
     if ($user instanceof EmailAwareUserInterface) {
         if (!$user->getEmail()) {
             return false;
         }
         $subject = $twigTemplate->renderBlock('subject', $parameters);
         if (!$subject) {
             throw new \Exception('Subject not defined');
         }
         $body = $twigTemplate->renderBlock('body_email_text', $parameters);
         $emailMessage = \Swift_Message::newInstance();
         $emailMessage = $emailMessage->setSubject($subject)->setFrom($this->sender)->setTo($user->getEmail())->setBody($body);
         /** @var \Swift_Message $emailMessage */
         $emailMessage = $emailMessage->addPart($text, 'text/html');
         return $this->mailer->send($emailMessage);
     }
     return null;
 }
 /**
  * @inheritdoc
  */
 public function renderCell(ListingColumnTypeInterface $column, $row)
 {
     if (!$this->loaded) {
         $this->load();
     }
     // Load and process value:
     $values = $column->getValues($row);
     //$value = $this->normalizeValue($value, $column->getOptions());
     // Create template block name and parameters:
     $blockName = 'listing_' . $column->getType();
     $parameters = array_merge(array('column' => $column, 'row' => $row), $values);
     // Render block:
     return trim($this->template->renderBlock($blockName, $parameters, $this->blocks));
 }
Esempio n. 10
0
 /**
  * @param string $email
  * @param array $parameters
  * @return \Swift_Message
  */
 private function preparedMessage($email, $parameters)
 {
     $message = new \Swift_Message();
     $message->setTo($email);
     $message->setFrom($this->template->renderBlock('from', $parameters), $this->template->renderBlock('from_name', $parameters));
     $message->setSubject($this->template->renderBlock('subject', $parameters));
     $message->setBody($this->template->renderBlock('body_text', $parameters));
     $message->addPart($this->template->renderBlock('body_html', $parameters), 'text/html');
     return $message;
 }
Esempio n. 11
0
 /**
  * Renders a Twig block.
  *
  * see {@link https://github.com/twigphp/Twig/issues/676#issuecomment-15842093}
  *
  * @param \Twig_Template $template
  * @param string         $block
  * @param array          $context
  *
  * @return string
  *
  * @throws \Exception
  */
 private function renderBlock(\Twig_Template $template, $block, array $context)
 {
     $context = $template->getEnvironment()->mergeGlobals($context);
     $level = ob_get_level();
     ob_start();
     try {
         $rendered = $template->renderBlock($block, $context);
         ob_end_clean();
         return $rendered;
     } catch (\Exception $e) {
         while (ob_get_level() > $level) {
             ob_end_clean();
         }
         throw $e;
     }
 }
 /**
  * Renders the HTML for a given field.
  *
  * @param \eZ\Publish\API\Repository\Values\Content\Content $content
  * @param string $fieldIdentifier Identifier for the field we want to render
  * @param array $params An array of parameters to pass to the field view
  *
  * @throws InvalidArgumentException
  * @return string The HTML markup
  */
 public function renderField(Content $content, $fieldIdentifier, array $params = array())
 {
     $field = $this->translationHelper->getTranslatedField($content, $fieldIdentifier, isset($params['lang']) ? $params['lang'] : null);
     if (!$field instanceof Field) {
         throw new InvalidArgumentException('$fieldIdentifier', "Invalid for content #{$content->contentInfo->id} '{$content->contentInfo->name}'");
     }
     $localTemplate = null;
     if (isset($params['template'])) {
         // local override of the template
         // this template is put on the top the templates stack
         $localTemplate = $params['template'];
         unset($params['template']);
     }
     $params = $this->getRenderFieldBlockParameters($content, $field, $params);
     // Getting instance of Twig_Template that will be used to render blocks
     if (!$this->template instanceof Twig_Template) {
         $tpl = reset($this->renderFieldResources);
         $this->template = $this->environment->loadTemplate($tpl['template']);
     }
     return $this->template->renderBlock($this->getRenderFieldBlockName($content, $field), $params, $this->getBlocksByField($content, $field, $localTemplate));
 }
Esempio n. 13
0
 /**
  * @param  \ServerGrove\KbBundle\Document\Article $article
  * @return string
  */
 public function renderArticleLocales(Article $article)
 {
     return $this->twig->renderBlock('article_locales', array('article' => $article, 'locales' => $this->locales));
 }
Esempio n. 14
0
 /**
  * Render block $block with $table view's data.
  * @param \Twig_Environment $twig
  * @param \EMC\TableBundle\Table\TableView $view
  * @param string $block
  * @return string
  */
 public function render(\Twig_Environment $twig, TableView $view, $block)
 {
     $this->load();
     return $this->template->renderBlock($block, $view->getData());
 }
Esempio n. 15
0
 /**
  * @param \SimpleXMLIterator $element
  * @param \Twig_Template     $template
  *
  * @param string             $formId
  * @param string             $xPath
  * @param string             $requiredChildren
  * @param array              $identityRefs
  *
  * @return array|bool
  */
 public function getChildrenValues($element, $template, $formId, $xPath = "", $requiredChildren = "", $identityRefs = array())
 {
     $retArr = array();
     $targetAttributes = array('key', 'iskey', 'mandatory');
     foreach ($element as $label => $el) {
         $attributesArr = array_fill_keys($targetAttributes, false);
         foreach ($element->attributes() as $name => $attr) {
             if ($name == "key") {
                 $attributesArr[$name] = (string) $attr[0];
             }
         }
         foreach ($el->attributes() as $name => $attr) {
             if (in_array($name, array('iskey', 'mandatory'))) {
                 $attributesArr[$name] = (string) $attr[0];
             }
         }
         if (($attributesArr['iskey'] !== "true" && $attributesArr['key'] == false || $requiredChildren !== "" && $label != $requiredChildren) && $attributesArr['mandatory'] == false) {
             continue;
         }
         if ($attributesArr['key'] !== false) {
             $requiredChildren = $attributesArr['key'];
         } else {
             $requiredChildren = "";
         }
         $twigArr = array();
         $twigArr['key'] = "";
         $twigArr['xpath'] = "";
         $twigArr['element'] = $el;
         $twigArr['useHiddenInput'] = true;
         $twigArr['moduleIdentityRefs'] = $identityRefs;
         $newXPath = $xPath . "/*";
         $res = $this->getAvailableLabelValuesForXPath($formId, $newXPath);
         $retArr[$label] = array();
         if (isset($res['labelsAttributes'][$label])) {
             $retArr[$label]['labelAttributes'] = $res['labelsAttributes'][$label];
         }
         $retArr[$label]['valueElem'] = $this->removeMultipleWhitespaces($template->renderBlock('configInputElem', $twigArr));
         $retArr[$label]['children'] = $this->getChildrenValues($el, $template, $formId, $newXPath, $requiredChildren);
     }
     return sizeof($retArr) ? $retArr : false;
 }
 /**
  * @param string $block
  * @param array  $context
  *
  * @return string
  */
 private function renderTemplateBlock($block, array $context)
 {
     $this->loadTemplate();
     return $this->template->renderBlock($block, $context);
 }
Esempio n. 17
-1
 public function formMessages(FormViewInterface $form)
 {
     return $this->baseTemplate->renderBlock('messages', array('form' => $form));
 }