本文整理汇总了PHP中Twig_Environment::loadTemplate方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::loadTemplate方法的具体用法?PHP Twig_Environment::loadTemplate怎么用?PHP Twig_Environment::loadTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::loadTemplate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: send
/**
* @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);
}
示例3: __construct
/**
* 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;
}
}
示例4: 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;
}
示例5: 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();
}
示例6: setUp
/**
* {@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');
}
示例7: _getTemplateResource
protected final function _getTemplateResource($template)
{
if (!isset($this->__templateResources[$template])) {
$this->__templateResources[$template] = $this->environment->loadTemplate($template);
}
return $this->__templateResources[$template];
}
示例8: getIconTemplate
/**
* @return \Twig_TemplateInterface $template
*/
protected function getIconTemplate()
{
if ($this->iconTemplate === null) {
$this->iconTemplate = $this->environment->loadTemplate('@MopaBootstrap/icons.html.twig');
}
return $this->iconTemplate;
}
示例9: onRequestedPassword
/**
* @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);
}
示例10: loadFile
/**
* @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;
}
示例11: getTemplates
/**
* 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;
}
示例12: renderIndexablePageParts
/**
* @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);
}
示例13: 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);
}
示例14: run
public function run()
{
$scanner = new ModuleScanner();
$modules = $scanner->getModules();
$template = $this->twig->loadTemplate("modules.twig");
$output = $template->render(["modules" => $modules]);
print $output;
}
示例15: 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);
}