/** * @return Template */ public function createTemplate(UI\Control $control = NULL) { $latte = $this->latteFactory->create(); $template = new Template($latte); $presenter = $control ? $control->getPresenter(FALSE) : NULL; if ($control instanceof UI\Presenter) { $latte->setLoader(new Loader($control)); } if ($latte->onCompile instanceof \Traversable) { $latte->onCompile = iterator_to_array($latte->onCompile); } array_unshift($latte->onCompile, function ($latte) use($control, $template) { $latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler())); UIMacros::install($latte->getCompiler()); if (class_exists(Nette\Bridges\FormsLatte\FormMacros::class)) { Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler()); } if ($control) { $control->templatePrepareFilters($template); } }); $latte->addFilter('url', 'rawurlencode'); // back compatiblity foreach (['normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse'] as $name) { $latte->addFilter($name, 'Nette\\Utils\\Strings::' . $name); } $latte->addFilter('null', function () { }); $latte->addFilter('modifyDate', function ($time, $delta, $unit = NULL) { return $time == NULL ? NULL : Nette\Utils\DateTime::from($time)->modify($delta . $unit); // intentionally == }); if (!isset($latte->getFilters()['translate'])) { $latte->addFilter('translate', function (Latte\Runtime\FilterInfo $fi) { throw new Nette\InvalidStateException('Translator has not been set. Set translator using $template->setTranslator().'); }); } // default parameters $template->user = $this->user; $template->baseUri = $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/') : NULL; $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl); $template->flashes = []; if ($control) { $template->control = $control; $template->presenter = $presenter; $latte->addProvider('uiControl', $control); $latte->addProvider('uiPresenter', $presenter); $latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($control)); } $latte->addProvider('cacheStorage', $this->cacheStorage); // back compatibility $template->_control = $control; $template->_presenter = $presenter; $template->netteCacheStorage = $this->cacheStorage; if ($presenter instanceof UI\Presenter && $presenter->hasFlashSession()) { $id = $control->getParameterId('flash'); $template->flashes = (array) $presenter->getFlashSession()->{$id}; } return $template; }
public function createTemplate($class = NULL) { $latte = $this->latteFactory->create(); $template = $class ? new $class($latte) : new Template($latte); $template->getLatte()->addFilter(NULL, 'Nette\\Templating\\Helpers::loader'); if ($latte->onCompile instanceof Traversable) { $latte->onCompile = iterator_to_array($latte->onCompile); } array_unshift($latte->onCompile, function ($latte) { $latte->getParser()->shortNoEscape = TRUE; $latte->getCompiler()->addMacro('cache', new CacheMacro($latte->getCompiler())); UIMacros::install($latte->getCompiler()); FormMacros::install($latte->getCompiler()); }); $latte->addFilter('url', 'rawurlencode'); // back compatiblity foreach (array('normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse') as $name) { $latte->addFilter($name, 'Nette\\Utils\\Strings::' . $name); } $template->user = $this->user; $template->netteHttpResponse = $this->httpResponse; $template->netteCacheStorage = $this->httpRequest; $template->baseUri = $template->baseUrl = rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/'); $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl); if (!isset($template->flashes) || !is_array($template->flashes)) { $template->flashes = array(); } $template->setTranslator($this->translator); $template->_control = $this->linker; return $template; }
public function getCompiler() { $compiler = parent::getCompiler(); if (!$this->fixed) { $this->fixed = TRUE; $compiler->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($compiler)); Nette\Bridges\ApplicationLatte\UIMacros::install($compiler); Nette\Bridges\FormsLatte\FormMacros::install($compiler); } return $compiler; }
/** * @return Template */ public function createTemplate(UI\Control $control) { $latte = $this->latteFactory->create(); $template = new Template($latte); $presenter = $control->getPresenter(FALSE); if ($control instanceof UI\Presenter) { $latte->setLoader(new Loader($control)); } if ($latte->onCompile instanceof \Traversable) { $latte->onCompile = iterator_to_array($latte->onCompile); } array_unshift($latte->onCompile, function ($latte) use ($control, $template) { $latte->getParser()->shortNoEscape = TRUE; $latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler())); UIMacros::install($latte->getCompiler()); if (class_exists('Nette\Bridges\FormsLatte\FormMacros')) { Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler()); } $control->templatePrepareFilters($template); }); $latte->addFilter('url', 'rawurlencode'); // back compatiblity foreach (array('normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse') as $name) { $latte->addFilter($name, 'Nette\Utils\Strings::' . $name); } $latte->addFilter('null', function () {}); $latte->addFilter('length', function ($var) { return is_string($var) ? Nette\Utils\Strings::length($var) : count($var); }); $latte->addFilter('modifyDate', function ($time, $delta, $unit = NULL) { return $time == NULL ? NULL : Nette\Utils\DateTime::from($time)->modify($delta . $unit); // intentionally == }); // default parameters $template->control = $template->_control = $control; $template->presenter = $template->_presenter = $presenter; $template->user = $this->user; $template->netteHttpResponse = $this->httpResponse; $template->netteCacheStorage = $this->cacheStorage; $template->baseUri = $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/') : NULL; $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl); $template->flashes = array(); if ($presenter instanceof UI\Presenter && $presenter->hasFlashSession()) { $id = $control->getParameterId('flash'); $template->flashes = (array) $presenter->getFlashSession()->$id; } return $template; }
/** * @param Message $mail * @param string $body * @param array|NULL $params */ public function send(Message $mail, $body = NULL, array $params = NULL) { if ($body) { $latte = $this->latteFactory->create(); $latte->setLoader(new StringLoader()); $latte->addProvider('uiControl', $this->linkGenerator); UIMacros::install($latte->getCompiler()); $params = (array) $params; // can be NULL $html = $latte->renderToString($body, $params); $mail->setHtmlBody($html); } $this->mailer->send($mail); }
/** * Creates template and registers helpers and latte filter * @param UI\Presenter $presenter * @param string $file Filepath to file * @param string|NULL $lang Lang code (length=2) * @param string $class Name of template class * @throws InvalidArgumentException * @return Nette\Templating\IFileTemplate */ public function createTemplate(UI\Presenter $presenter, $file = NULL, $lang = NULL, $class = NULL) { $this->translator->setLangTo($lang); $latte = $this->latteFactory->create(); $template = $class ? new $class($latte) : new Nette\Bridges\ApplicationLatte\Template($latte); $template->getLatte()->addFilter(NULL, 'Nette\\Templating\\Helpers::loader'); $template->getLatte()->addFilter(NULL, '\\Helpers::loader'); array_unshift($latte->onCompile, function ($latte) { $latte->getParser()->shortNoEscape = TRUE; $latte->getCompiler()->addMacro('cache', new CacheMacro($latte->getCompiler())); Nette\Bridges\ApplicationLatte\UIMacros::install($latte->getCompiler()); FormMacros::install($latte->getCompiler()); }); $latte->addFilter('url', 'rawurlencode'); // back compatiblity foreach (array('normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse') as $name) { $latte->addFilter($name, 'Nette\\Utils\\Strings::' . $name); } $template->setTranslator($this->translator); if (!is_null($file)) { $template->setFile($this->appDir . $file); } // default parameters $template->control = $template->_control = $presenter; $template->presenter = $template->_presenter = $presenter; if ($presenter instanceof UI\Presenter) { $template->user = $presenter->getUser(); $template->netteHttpResponse = $this->httpResponse; $template->netteCacheStorage = $this->netteCacheStorage; $template->baseUri = $template->baseUrl = rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/'); $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl); // flash message if ($presenter->hasFlashSession()) { $id = $presenter->getParameterId('flash'); $template->flashes = $presenter->getFlashSession()->{$id}; } } if (!isset($template->flashes) || !is_array($template->flashes)) { $template->flashes = array(); } return $template; }
/** * @return Template */ public function createTemplate(UI\Control $control) { $latte = $this->latteFactory->create(); $template = new Template($latte); $presenter = $control->getPresenter(FALSE); if ($control instanceof UI\Presenter) { $latte->setLoader(new Loader($control)); } if ($latte->onCompile instanceof \Traversable) { $latte->onCompile = iterator_to_array($latte->onCompile); } array_unshift($latte->onCompile, function ($latte) use($control, $template) { $latte->getParser()->shortNoEscape = TRUE; $latte->getCompiler()->addMacro('cache', new Nette\Bridges\CacheLatte\CacheMacro($latte->getCompiler())); UIMacros::install($latte->getCompiler()); Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler()); $control->templatePrepareFilters($template); }); $latte->addFilter('url', 'rawurlencode'); // back compatiblity foreach (array('normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse') as $name) { $latte->addFilter($name, 'Nette\\Utils\\Strings::' . $name); } // default parameters $template->control = $template->_control = $control; $template->presenter = $template->_presenter = $presenter; $template->user = $this->user; $template->netteHttpResponse = $this->httpResponse; $template->netteCacheStorage = $this->cacheStorage; $template->baseUri = $template->baseUrl = rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/'); $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl); $template->flashes = array(); if ($presenter instanceof UI\Presenter && $presenter->hasFlashSession()) { $id = $control->getParameterId('flash'); $template->flashes = (array) $presenter->getFlashSession()->{$id}; } return $template; }
function get($validate = TRUE, $parseSubject = TRUE) { $required = ['from', 'to', 'subject', 'content']; $args = ['from' => $this->from, 'fromName' => $this->fromName, 'to' => $this->to, 'toName' => $this->toName, 'replyTo' => is_null($this->replyTo) ? $this->from : $this->replyTo, 'replyToName' => is_null($this->replyTo) ? $this->fromName : $this->replyToName, 'subject' => $parseSubject ? $this->getSubject() : $this->subjectDefault, 'content' => $this->content, 'attachments' => $this->attachments]; if (!is_null($this->unsubscribeEmail)) { $args['unsubscribeEmail'] = $this->unsubscribeEmail; } if (!is_null($this->unsubscribeLink)) { $args['unsubscribeLink'] = $this->unsubscribeLink; } if (is_null($this->content) && !is_null($this->template)) { foreach ($this->templateArgsMinimum as $v) { if (!isset($this->templateArgs[$v])) { if ($validate) { trigger_error('Missing templateArgs.' . $v); $this->templateArgs[$v] = ''; } } } if ($validate) { $latte = $this->latteFactory->create(); $latte->addProvider('uiControl', $this->linkGenerator); $latte->addProvider('uiPresenter', $this->linkGenerator); $args['_url'] = $this->refUrl; Nette\Bridges\ApplicationLatte\UIMacros::install($latte->getCompiler()); foreach ($this->latteSetupFilterCallback as $v) { $v($latte); } $args['content'] = $latte->renderToString($this->template, $args + $this->templateArgs); } } foreach ($required as $v) { if (is_null($args[$v])) { if ($validate) { trigger_error('Missing ' . $v); $args[$v] = ''; } } } return (object) $args; }
/** * @return Nette\Application\UI\Form */ protected function createComponentSignRememberForm() { $form = $this->signRememberFactory->create(); $form->onSuccess[] = function (Form $form) { $values = $form->getValues(); /** @var UsersEntity $user */ $user = null; if ($values->username > '') { $user = $this->usersRepository->getOneWhere(['username' => $values->username]); } if (is_null($user) && $values->email > '') { $user = $this->usersRepository->getOneWhere(['email' => $values->email]); } if (is_null($user)) { $this->flashMessage('Neznámý uživatel', 'error'); $this->redirect('this'); } $now = new DateTime(); $validity = new DateTime('+1 day'); $user->passwordHash(Passwords::hash($now . $user->email())); $user->passwordHashValidity($validity); $this->usersRepository->save($user); $message = new Message(); $params = ['username' => $user->username(), 'token' => $user->passwordHash(), '_presenter' => $this, '_control' => $this]; $latte = new Engine(); UIMacros::install($latte->getCompiler()); $message->setFrom($this->settings['adminMail'])->addTo($user->email())->setSubject('Zapomenuté heslo')->setHtmlBody($latte->renderToString(__DIR__ . '/templates/Sign/rememberMail.latte', $params)); $mailer = $this->setMailer(); $mailer->send($message); $this->flashMessage('Zkontrolujte si prosím svou mailovou schránku', 'success'); $this->redirect('Sign:in'); }; return $form; }