addFilter() public method

Registers run-time filter.
public addFilter ( $name, $callback ) : static
return static
Esempio n. 1
0
 public function register(Engine $engine)
 {
     if (class_exists('Latte\\Runtime\\FilterInfo')) {
         $engine->addFilter('translate', [$this, 'translateFilterAware']);
     } else {
         $engine->addFilter('translate', [$this, 'translate']);
     }
     $engine->addFilter('getTranslator', [$this, 'getTranslator']);
 }
Esempio n. 2
0
 /**
  * Renders HTML code for custom panel.
  *
  * @return string html
  * @internal
  */
 public function getPanel()
 {
     $latte = new Latte\Engine();
     $latte->addFilter('storageId', [$this, 'getStorageId']);
     $latte->addFilter('colorRange', [$this, 'getColorInRange']);
     $args = ['title' => $this->getTitle(), 'collector' => $this->collector];
     if ($this->collector->getQueries()) {
         $this->extremes = $this->collector->getTimeExtremes();
     }
     return $latte->renderToString(__DIR__ . '/queryPanel.latte', $args);
 }
Esempio n. 3
0
 public function register(Engine $engine)
 {
     $engine->addFilter('imageSize', function ($imagePath) {
         return $this->imageSize($imagePath);
     });
     $engine->addFilter('nl2ul', function ($text, $class) {
         return $this->nl2ul($text, $class);
     });
     $engine->addFilter('correctUrl', function ($url) {
         return $this->correctUrl($url);
     });
     $engine->addFilter('hideIpPart', function ($ip, $hideParts = 1) {
         return $this->hideIpPart($ip, $hideParts);
     });
 }
Esempio n. 4
0
 /**
  * Sets translate adapter.
  * @return self
  */
 public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
 {
     $this->latte->addFilter('translate', $translator === NULL ? NULL : function (Latte\Runtime\FilterInfo $fi, ...$args) use($translator) {
         return $translator->translate(...$args);
     });
     return $this;
 }
Esempio n. 5
0
/**
 * @return Engine
 */
function latte()
{
    $latte = new Engine();
    $latte->setLoader(filter('latte.loader', new FileLoader()));
    $latte->setTempDirectory(\dir\cache());
    $latte->addFilter('md', '\\cms\\md');
    trigger('latte.macroset', new MacroSet($latte->getCompiler()));
    return filter('latte', $latte);
}
Esempio n. 6
0
/**
 * @return Engine
 */
function latte()
{
    $latte = new Engine();
    $latte->setLoader(filter(FileLoader::class, new FileLoader()));
    $latte->setTempDirectory(\dir\cache());
    $latte->addFilter('md', '\\cms\\md');
    trigger(MacroSet::class, new MacroSet($latte->getCompiler()));
    return filter(Engine::class, $latte);
}
Esempio n. 7
0
 /**
  * @return Engine
  */
 public function create()
 {
     $engine = new Engine();
     // Options
     $engine->setTempDirectory($this->tempDir);
     $engine->setAutoRefresh($this->autoRefresh);
     $engine->setContentType($this->contentType);
     // Filters
     foreach ($this->filters as $name => $callback) {
         $engine->addFilter($name, $callback);
     }
     // Macros
     $engine->onCompile[] = function (Engine $engine) {
         $compiler = $engine->getCompiler();
         foreach ($this->macros as $macro) {
             $macro::install($compiler);
         }
     };
     return $engine;
 }
Esempio n. 8
0
 /**
  * @return Latte\Engine
  */
 private function getLatte()
 {
     if (!isset($this->latte)) {
         $this->latte = new Latte\Engine();
         $this->latte->setTempDirectory($this->tempDir);
         $this->latte->setAutoRefresh(FALSE);
         $this->latte->onCompile[] = function (Latte\Engine $latte) {
             $set = new Latte\Macros\MacroSet($latte->getCompiler());
             $set->addMacro('link', 'echo %escape(call_user_func($getLink, %node.word, %node.array))');
         };
         $this->latte->addFilter('attachmentLabel', function (MimePart $attachment) {
             $contentDisposition = $attachment->getHeader('Content-Disposition');
             $contentType = $attachment->getHeader('Content-Type');
             $matches = Strings::match($contentDisposition, '#filename="(.+?)"#');
             return ($matches ? "{$matches['1']} " : '') . "({$contentType})";
         });
         $this->latte->addFilter('plainText', function (MimePart $part) {
             $ref = new \ReflectionProperty('Nette\\Mail\\MimePart', 'parts');
             $ref->setAccessible(TRUE);
             $queue = array($part);
             for ($i = 0; $i < count($queue); $i++) {
                 /** @var MimePart $subPart */
                 foreach ($ref->getValue($queue[$i]) as $subPart) {
                     $contentType = $subPart->getHeader('Content-Type');
                     if (Strings::startsWith($contentType, 'text/plain') && $subPart->getHeader('Content-Transfer-Encoding') !== 'base64') {
                         // Take first available plain text
                         return (string) $subPart->getBody();
                     } elseif (Strings::startsWith($contentType, 'multipart/alternative')) {
                         $queue[] = $subPart;
                     }
                 }
             }
             return $part->getBody();
         });
     }
     return $this->latte;
 }
Esempio n. 9
0
 public function install(Engine $latte)
 {
     $latte->addFilter('formatTime', function ($value) {
         return $value instanceof \DateTime ? $value->format('H:i:s') : $value;
     });
     $latte->addFilter('formatDate', function ($value) {
         return $value instanceof \DateTime ? $value->format('j.n.Y') : $value;
     });
     $latte->addFilter('formatDateTime', function ($value) {
         return $value instanceof \DateTime ? $value->format('j.n.Y H:i:s') : $value;
     });
     $latte->addFilter('formatNumber', function ($value) {
         if ($value === NULL) {
             return $value;
         }
         return str_replace('.', ',', $value);
     });
     $latte->addFilter('yesNo', function ($value, $yes = 'Ano', $no = 'Ne') {
         return $value ? $yes : $no;
     });
     $latte->addFilter('mTime', function ($path) {
         return filemtime($this->wwwDir . DIRECTORY_SEPARATOR . $path);
     });
 }
Esempio n. 10
0
 protected function createLatte()
 {
     $latte = new Latte\Engine();
     foreach ($this->helpers as $key => $callback) {
         $latte->addFilter($key, $callback);
     }
     foreach ($this->helperLoaders as $callback) {
         $latte->addFilter(NULL, function ($name) use($callback, $latte) {
             if ($res = call_user_func($callback, $name)) {
                 $latte->addFilter($name, $res);
             }
         });
     }
     if ($this->cacheStorage instanceof Nette\Caching\Storages\PhpFileStorage) {
         $latte->setTempDirectory($this->cacheStorage->getDir());
     }
     return $latte;
 }
Esempio n. 11
0
 /**
  * Renders loaded files table.
  * @return string
  */
 private function getTable()
 {
     $latte = new Latte\Engine();
     $latte->addFilter('extension', function ($extension) {
         return isset(Panel::$types[$extension]) ? Panel::$types[$extension] : $extension;
     });
     return $latte->renderToString(__DIR__ . '/panel.latte', array('files' => $this->files, 'sizes' => $this->sizes, 'size' => $this->size));
 }
Esempio n. 12
0
 public function load(Engine $latte)
 {
     foreach ($this->helpers as $name => $callback) {
         $latte->addFilter($name, $callback);
     }
 }
Esempio n. 13
0
 public function register(Engine $engine)
 {
     $engine->addFilter('dateCzech', function ($date, $format) {
         return $this->dateCzech($date, $format);
     });
     $engine->addFilter('dateLocalized', function ($date) {
         return $this->dateLocalized($date);
     });
     $engine->addFilter('monthCzech', function ($monthNumber) {
         return $this->monthCzech($monthNumber);
     });
     $engine->addFilter('timeAgoInWords', function ($time) {
         return $this->timeAgoInWords($time);
     });
     $engine->addFilter('dateTodayYesterday', function ($date, $formatShort = 'H:i', $formatLong = 'd.m. H:i') {
         return $this->dateTodayYesterday($date, $formatShort, $formatLong);
     });
     $engine->addFilter('czechHoliday', function ($date, $format, $separator = ' - ') {
         return $this->czechHoliday($date, $format, $separator);
     });
 }
 public function register(Engine $engine)
 {
     $engine->addFilter('translate', array($this, 'translate'));
     $engine->addFilter('getTranslator', array($this, 'getTranslator'));
 }
Esempio n. 15
0
 /**
  * @param string $view email template
  * @param User $user
  * @param NULL|array $args template variables
  *
  * @throws Exception from Latte\Engine
  * @throws SmtpException from Mailer
  */
 public function send($view, User $user, array $args = [])
 {
     if ($this->orm->unsubscribes->getByEmail($user->email)) {
         // Last line of defense. Make sure unsubscribed users
         // really dont receive any email. This should however
         // be handled before the task is queued.
         $this->logger->addAlert("Email to '{$user->email}' not send, user unsubscribed. Find out why it was queued");
         return;
     }
     $msg = new Message();
     $msg->setFrom('Khanova škola <*****@*****.**>');
     $msg->addReplyTo('Markéta Matějíčková <*****@*****.**>');
     $msg->addTo($user->email, $user->name);
     $token = Unsubscribe::createFromUser($user);
     $token->emailType = $view;
     $this->orm->tokens->attach($token);
     $this->orm->flush();
     $args['recipient'] = $user;
     $args['email'] = $msg;
     $args['unsubscribe'] = (object) ['token' => $token, 'code' => $token->getUnsafe()];
     $args['baseUrl'] = rtrim($this->baseUrl, '/');
     $latte = new Engine();
     /** @var Presenters\Token $presenter */
     $presenter = $this->factory->createPresenter('Token');
     $presenter->autoCanonicalize = FALSE;
     $ref = new \ReflectionProperty(Presenter::class, 'globalParams');
     $ref->setAccessible(TRUE);
     $ref->setValue($presenter, []);
     $latte->addFilter('token', function (Token $token, $unsafe) use($presenter, $view) {
         return $presenter->link('//Token:', ['token' => $token->toString($unsafe), 'utm_campaign' => "email-{$view}"]);
     });
     $latte->addFilter('vocative', function ($phrase) {
         return $this->inflection->inflect($phrase, 5);
     });
     $template = $latte->renderToString($this->getTemplate($view), $args);
     $msg->setHtmlBody($template);
     $this->mailer->send($msg);
     $this->logger->addInfo('Email send', ['view' => $view, 'email' => $user->email]);
 }
Esempio n. 16
0
 /**
  * Register template filters
  *
  * @param Engine $engine
  */
 public function register(Engine $engine)
 {
     $engine->addFilter('phone', [$this, 'phone']);
     $engine->addFilter('getPhoneNumberService', [$this, 'getPhoneNumberService']);
 }
Esempio n. 17
0
	/**
	 * Sets translate adapter.
	 * @return self
	 */
	public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
	{
		$this->latte->addFilter('translate', $translator === NULL ? NULL : array($translator, 'translate'));
		return $this;
	}
Esempio n. 18
0
 private function createTemplate()
 {
     $latte = new Latte\Engine();
     $latte->addFilter('ordinal', function ($n) {
         switch (substr($n, -1)) {
             case 1:
                 return 'st';
             case 2:
                 return 'nd';
             case 3:
                 return 'rd';
             default:
                 return 'th';
         }
     });
     return $latte;
 }