/** * @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; }
/** * @return \Nette\Bridges\ApplicationLatte\Template */ protected function createTemplate() { $latte = new Latte\Engine(); $latte->onCompile[] = function ($latte) { Bridges\FormsLatte\FormMacros::install($latte->getCompiler()); }; return new Bridges\ApplicationLatte\Template($latte); }
/** * @return Latte\Engine */ protected function createLatte() { $latte = $this->latteFactory->create(); // Base macros Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler()); // Button macros ButtonMacros::install($latte->getCompiler()); return $latte; }
/** * @return Latte\Engine * @throws \NForms\Exceptions\InvalidStateException */ protected function createLatte() { if (!$this->latteFactory) { throw new InvalidStateException("Please inject Latte factory."); } $latte = $this->latteFactory->create(); FormMacros::install($latte->getCompiler()); ButtonMacros::install($latte->getCompiler()); return $latte; }
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; }
/** * Setting templating system. * @return Engine */ private function createLatteEngine() { // Create directory. $temp = $this->temp . '/_Latte.TemplateCache'; if (!is_dir($temp)) { FileSystem::createDir($temp); } $latte = new Engine(); $latte->setTempDirectory($temp); // Macro for forms. $latte->onCompile[] = function ($latte) { FormMacros::install($latte->getCompiler()); }; return $latte; }
/** * 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; }
/** * @param Form $form * @param array $args */ private static function renderFormBegin(Form $form, array $args) { if ($form->getRenderer() instanceof BootstrapRenderer) { $form->render('begin', $args); } else { Nette\Bridges\FormsLatte\FormMacros::renderFormBegin($form, $args); } }
/** * Render the templates. * @param Form $form * @param string $mode * @param array $args */ public function render(Form $form, $mode = NULL, $args = NULL) { if ($this->template === NULL) { if ($presenter = $form->lookup('Nette\\Application\\UI\\Presenter', FALSE)) { /** @var \Nette\Application\UI\Presenter $presenter */ $this->template = clone $presenter->getTemplate(); } else { $this->template = new FileTemplate(); $this->template->registerFilter(new \Nette\Latte\Engine()); } } if ($this->form !== $form) { $this->form = $form; // translators if ($translator = $this->form->getTranslator()) { $this->template->setTranslator($translator); } // controls placeholders & classes foreach ($this->form->getControls() as $control) { $this->prepareControl($control); } $formEl = $form->getElementPrototype(); if (!($classes = self::getClasses($formEl)) || stripos($classes, 'form-') === FALSE) { //$formEl->addClass('form-horizontal'); } } elseif ($mode === 'begin') { foreach ($this->form->getControls() as $control) { /** @var \Nette\Forms\Controls\BaseControl $control */ $control->setOption('rendered', FALSE); } } $this->template->setFile(__DIR__ . '/@form.latte'); $this->template->setParameters(array_fill_keys(array('control', '_control', 'presenter', '_presenter'), NULL) + array('_form' => $this->form, 'form' => $this->form, 'renderer' => $this)); if ($mode === NULL) { if ($args) { $this->form->getElementPrototype()->addAttributes($args); } $this->template->render(); } elseif ($mode === 'begin') { FormMacros::renderFormBegin($this->form, (array) $args); } elseif ($mode === 'end') { FormMacros::renderFormEnd($this->form); } else { $attrs = array('input' => array(), 'label' => array()); foreach ((array) $args as $key => $val) { if (stripos($key, 'input-') === 0) { $attrs['input'][substr($key, 6)] = $val; } elseif (stripos($key, 'label-') === 0) { $attrs['label'][substr($key, 6)] = $val; } } $this->template->setFile(__DIR__ . '/@parts.latte'); $this->template->mode = $mode; $this->template->attrs = (array) $attrs; $this->template->render(); } }
/** * Render the templates * * @param \Nette\Forms\Form $form * @param string $mode * @param array $args * @return void */ public function render(Nette\Forms\Form $form, $mode = NULL, $args = NULL) { if ($this->template === NULL) { if ($presenter = $form->lookup('Nette\\Application\\UI\\Presenter', FALSE)) { /** @var \Nette\Application\UI\Presenter $presenter */ $this->template = clone $presenter->getTemplate(); } else { $this->template = new Nette\Bridges\ApplicationLatte\Template(new Nette\Latte\Engine()); } } if ($this->form !== $form) { $this->form = $form; // translators if ($translator = $this->form->getTranslator()) { $this->template->setTranslator($translator); } // controls placeholders & classes foreach ($this->form->getControls() as $control) { $this->prepareControl($control); } $formEl = $form->getElementPrototype(); if (!($classes = self::getClasses($formEl)) || stripos($classes, 'form-') === FALSE) { $this->horizontalMode = $this->mode === self::MODE_HORIZONTAL; if ($this->mode !== self::MODE_NO_CLASS) { $formEl->addClass($this->mode); } } } elseif ($mode === 'begin') { foreach ($this->form->getControls() as $control) { /** @var \Nette\Forms\Controls\BaseControl $control */ $control->setOption('rendered', FALSE); } } $this->template->setFile(__DIR__ . '/@form.latte'); $this->template->setParameters(array_fill_keys(array('control', '_control', 'presenter', '_presenter'), NULL) + array('_form' => $this->form, 'form' => $this->form, 'renderer' => $this)); if ($this->horizontalMode) { $this->template->labelCols = $this->labelColumns; $this->template->inputCols = $this->inputColumns; $this->template->labelClass = $this->columnClassPrefix . $this->labelColumns; $this->template->inputClass = $this->columnClassPrefix . $this->inputColumns; $this->template->skipClass = $this->columnClassPrefix . 'offset-' . $this->labelColumns; } if ($mode === NULL) { if ($args) { $this->form->getElementPrototype()->addAttributes($args); } $this->template->render(); } elseif ($mode === 'begin') { FormMacros::renderFormBegin($this->form, (array) $args); } elseif ($mode === 'end') { FormMacros::renderFormEnd($this->form); } else { $attrs = array('input' => array(), 'label' => array(), 'pair' => array(), 'pair-class' => ''); foreach ((array) $args as $key => $val) { if (stripos($key, 'input-') === 0) { $attrs['input'][substr($key, 6)] = $val; } elseif (stripos($key, 'label-') === 0) { $attrs['label'][substr($key, 6)] = $val; } elseif ($key === 'class') { $attrs['pair-class'] = $val; } else { $attrs['pair'][$key] = $val; } } if ($this->horizontalMode) { if (isset($attrs['label']['class'])) { $attrs['label']['class'] .= ' ' . $this->columnClassPrefix . $this->labelColumns; } else { $attrs['label']['class'] = $this->columnClassPrefix . $this->labelColumns; } } $this->template->setFile(__DIR__ . '/@parts.latte'); $this->template->mode = $mode; $this->template->attrs = (array) $attrs; $this->template->render(); } }
function createTemplate(\Nette\Application\UI\Control $control = NULL) { $latte = new \Latte\Engine(); \Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler()); return new \Nette\Bridges\ApplicationLatte\Template($latte); }
public function macroInput(MacroNode $node, PhpWriter $writer) { return $this->getBeforeRenderCalling($writer) . parent::macroInput($node, $writer); }