/** * 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'); }
/** * 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)); } }
/** * @param Template $theme * @param string $block * @return Template | null */ protected function tryToCache(Template $theme, $block) { if ($theme->hasBlock($block)) { $this->cache[$block] = $theme; return $theme; } }
protected function getBlock(\Twig_Template $template, $name) { if ($template->hasBlock($name)) { $blocks = $template->getBlocks(); return $blocks[$name]; } elseif (false !== ($parent = $template->getParent([]))) { return $this->getBlock($parent, $name); } return false; }
/** * @param \Twig_Template $template * @param string $blockName * @return \Twig_Template|bool */ private function findTemplateWithBlock(\Twig_Template $template, $blockName) { if ($template->hasBlock($blockName)) { return $template; } // Check parents if (false !== ($parent = $template->getParent(array()))) { if ($this->findTemplateWithBlock($parent, $blockName) !== false) { return $template; } } return false; }
/** * @param \Twig_Template $template * @param string $blockName * * @return \Twig_Template|bool */ private function findTemplateWithBlock(\Twig_Template $template, $blockName) { if ($template->hasBlock($blockName)) { return $template; } if (false === ($parent = $template->getParent([]))) { return false; } if (false !== $this->findTemplateWithBlock($parent, $blockName)) { return $template; } return false; }