本文整理汇总了PHP中Twig_Environment::mergeGlobals方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::mergeGlobals方法的具体用法?PHP Twig_Environment::mergeGlobals怎么用?PHP Twig_Environment::mergeGlobals使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::mergeGlobals方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sendMail
/**
* 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;
}
示例2: render
/**
* {@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();
}
示例3: create
/**
* {@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;
}
示例4: sendNotification
/**
* 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);
}
示例5: send
/**
* {@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);
}
示例6: provideEmailWithTemplate
/**
* @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);
}
示例7: sendMessage
/**
* 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);
}
示例8: sendNotification
/**
* 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);
}
示例9: renderBlock
/**
* {@inheritdoc}
*/
public function renderBlock(FormView $view, $resource, $blockName, array $variables = array())
{
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
$context = $this->environment->mergeGlobals($variables);
ob_start();
// By contract,This method can only be called after getting the resource
// (which is passed to the method). Getting a resource for the first time
// (with an empty cache) is guaranteed to invoke loadResourcesFromTheme(),
// where the property $template is initialized.
// We do not call renderBlock here to avoid too many nested level calls
// (XDebug limits the level to 100 by default)
$this->template->displayBlock($blockName, $context, $this->resources[$cacheKey]);
return ob_get_clean();
}
示例10: loadTemplate
/**
* Loads the template.
*
* @param string $template The template relative path
* @param array $vars The template variables
*
* @throws TemplateNotFoundException When template does not exist
* @throws UnsupportedTemplateException When template format is not supported
*
* @return TemplateInterface
*/
public function loadTemplate($template, array $vars = [])
{
if (!$this->supports($template)) {
throw new UnsupportedTemplateException(sprintf('Template %s is not supported by this engine.', $template));
}
try {
$reference = $this->twig->resolveTemplate($template);
} catch (\Twig_Error_Loader $exception) {
throw new TemplateNotFoundException(sprintf('Template %s does not exist.', $template), $exception);
} catch (\Exception $exception) {
throw new UnsupportedTemplateException(sprintf('Invalid template %s provided.', $template), $exception);
}
return new Template($reference->getTemplateName(), $this->twig->mergeGlobals($vars));
}
示例11: sendMessage
/**
* sendMessage.
*
* @param string $templateName
* @param array $context
* @param string $toEmail
*/
private function sendMessage($templateName, array $context, $toEmail)
{
$context = $this->twig->mergeGlobals($context);
$template = $this->twig->loadTemplate($templateName);
$subject = $template->renderBlock('subject', $context);
$textBody = $template->renderBlock('body_text', $context);
$htmlBody = $template->renderBlock('body_html', $context);
$message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($this->fromEmail)->setTo($toEmail);
if (!empty($htmlBody)) {
$message->setBody($htmlBody, 'text/html')->addPart($textBody, 'text/plain');
} else {
$message->setBody($textBody);
}
$this->mailer->send($message);
}
示例12: sendMessage
/**
* @param string $templateName
* @param array $context
* @param string $fromEmail
* @param string $toEmail
*/
protected function sendMessage($templateName, $context, $toEmail)
{
$fromEmail = $this->parameters['from_email'];
$context = $this->twig->mergeGlobals($context);
$template = $this->twig->loadTemplate($templateName);
$subject = $template->renderBlock('subject', $context);
$textBody = trim($template->renderBlock('text_body', $context));
$htmlContent = trim($template->renderBlock('content_html_body', $context));
$htmlBody = trim($template->renderBlock('html_body', $context));
$message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($fromEmail['address'], $fromEmail['sender_name'])->setTo($toEmail);
$message->setBody($textBody);
if (!empty($htmlBody) && !empty($htmlContent)) {
$message->addPart($htmlBody, 'text/html');
}
$this->mailer->send($message);
}
示例13: sendMessage
/**
* @param string $templateName
* @param array $context
* @param string|array $fromEmail
* @param string|array $toEmail
*/
protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
{
if ($this->noSend) {
return;
}
$context = $this->twig->mergeGlobals($context);
$template = $this->twig->loadTemplate($templateName);
$subject = $template->renderBlock('subject', $context);
$textBody = $template->renderBlock('body_text', $context);
$htmlBody = $template->renderBlock('body_html', $context);
/*
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail);
if (!empty($htmlBody)) {
$message->setBody($htmlBody, 'text/html')
->addPart($textBody, 'text/plain');
} else {
$message->setBody($textBody);
}
$this->mailer->send($message);
*/
mail($toEmail, $subject, $htmlBody, "From: {$fromEmail}" . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n");
}
示例14: loadResourcesFromTheme
/**
* Loads the resources for all blocks in a theme.
*
* @param string $cacheKey The cache key for storing the resource.
* @param mixed $theme The theme to load the block from. This parameter
* is passed by reference, because it might be necessary
* to initialize the theme first. Any changes made to
* this variable will be kept and be available upon
* further calls to this method using the same theme.
*/
protected function loadResourcesFromTheme($cacheKey, &$theme)
{
if (!$theme instanceof \Twig_Template) {
/* @var \Twig_Template $theme */
$theme = $this->environment->loadTemplate($theme);
}
if (null === $this->template) {
// Store the first \Twig_Template instance that we find so that
// we can call displayBlock() later on. It doesn't matter *which*
// template we use for that, since we pass the used blocks manually
// anyway.
$this->template = $theme;
}
// Use a separate variable for the inheritance traversal, because
// theme is a reference and we don't want to change it.
$currentTheme = $theme;
$context = $this->environment->mergeGlobals(array());
// The do loop takes care of template inheritance.
// Add blocks from all templates in the inheritance tree, but avoid
// overriding blocks already set.
do {
foreach ($currentTheme->getBlocks() as $block => $blockData) {
if (!isset($this->resources[$cacheKey][$block])) {
// The resource given back is the key to the bucket that
// contains this block.
$this->resources[$cacheKey][$block] = $blockData;
}
}
} while (false !== ($currentTheme = $currentTheme->getParent($context)));
}
示例15: getRenderedEmail
/**
* @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);
}