/**
  * 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;
 }