/**
  * 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();
     }
 }
Пример #2
0
 public function prepareFilters(Template $template)
 {
     $this->latte = $this->latteFactory->invoke();
     foreach ($this->macroFactories as $factory) {
         $this->container->{Container::getMethodName($factory, FALSE)}($this->latte->getCompiler());
     }
     $template->registerFilter($this->latte);
 }
Пример #3
0
 /**
  * @param \Nette\Templating\Template|string|null $template full path to file and/or template instance and/or null
  * @param \Nette\Caching\IStorage|null $storage optional cache storage for template
  */
 public function __construct($template = null, \Nette\Caching\IStorage $storage = null)
 {
     if ($template === null) {
         $template = __DIR__ . '/bootstrap.latte';
     }
     if ($template instanceof \Nette\Templating\Template) {
         $this->template = $template;
     } elseif (is_string($template) && is_readable($template)) {
         $this->template = new \Nette\Templating\FileTemplate($template);
         $this->template->registerFilter(new \Nette\Latte\Engine());
     } elseif (is_string($template) && !is_readable($template)) {
         throw new \Nette\FileNotFoundException('File "' . $template . '" does not exists or is not readable by php.');
     } else {
         throw new \Nette\InvalidArgumentException('$template can be instance of Nette\\Templating\\Template, string path to file or null, ' . gettype($template) . ' given.');
     }
     if ($storage) {
         $this->template->setCacheStorage($storage);
     }
 }
Пример #4
0
 /**
  * @param string|Template $template Set either to constant, your instance of ITemplate and/or your path to your template file.
  * @param \Nette\Caching\IStorage $storage storage if available
  * @throws InvalidArgumentException Thrown when parameter combination does not match
  * @throws FileNotFoundException Thrown when path to the file is nonexistent or the file is not readable
  */
 public function __construct($template, \Nette\Caching\IStorage $storage = null)
 {
     $this->template = null;
     // If we're passed an ITemplate instance, use that
     if ($template instanceof ITemplate) {
         $this->template = $template;
     } elseif (is_string($template) && !file_exists($template) && file_exists(__DIR__ . '/' . $template)) {
         $template = __DIR__ . '/' . $template;
     }
     // CHeck & init
     if ($this->template === null && !file_exists($template)) {
         throw new InvalidArgumentException('$template has to be either path to template or instance of \\Nette\\Templating\\ITemplate');
     }
     if ($this->template === null) {
         $this->template = new FileTemplate($template);
         $this->template->registerFilter(new Engine());
         if ($storage) {
             $this->template->setCacheStorage($storage);
         }
     }
 }
Пример #5
0
 public function createNewTemplate($fileName = NULL)
 {
     $template = NULL;
     if ($fileName !== NULL) {
         $template = new \Nette\Templating\FileTemplate();
         $template->setFile($fileName);
     } else {
         $template = new Nette\Templating\Template();
     }
     $template->setTranslator($this->getPresenter()->context->translator);
     $template->registerFilter(new \Nette\Latte\Engine());
     $template->registerHelperLoader('Nette\\Templating\\Helpers::loader');
     //$baseUrl = rtrim($this->presenter->context->httpRequest->getUrl()->getBaseUrl(), '/');
     $template->basePath = $this->getBasePath();
     $template->themePath = $template->basePath . '/' . strtolower($this->presenter->pageManagerService->getCurrentModule());
     $template->_presenter = $this->presenter;
     $template->_control = $this;
     return $template;
 }
Пример #6
0
 private function _avelancheTransform($text, $filterParams = NULL, $templateParams = NULL)
 {
     //dump($filterParams);
     $_temp = $text;
     $hash = NULL;
     $oldHash = NULL;
     for ($i = 1; $i <= 50; $i++) {
         $template = new \Nette\Templating\Template();
         $template->setTranslator($this->presenter->context->translator);
         $template->setSource($_temp);
         $template->add('_presenter', $this->presenter);
         $template->add('_control', $this->presenter);
         $template->add('_page', $this);
         if ($templateParams !== NULL) {
             foreach ($templateParams as $k => $v) {
                 $template->add($k, $v);
             }
         }
         $template->onPrepareFilters[] = function ($template) use($filterParams) {
             $template->registerFilter(new Bubo\Filters\CMSFilter($filterParams));
         };
         $template->onPrepareFilters[] = function ($template) {
             $template->registerFilter(new \Nette\Latte\Engine());
         };
         //$template->setCacheStorage(new \Nette\Caching\Storages\PhpFileStorage(TEMP_DIR.'/cache'));
         $_temp = $template->__toString();
         if (($hash = sha1($_temp)) == $oldHash) {
             break;
         }
         $oldHash = $hash;
     }
     return $_temp;
 }
Пример #7
0
 /**
  * Load latte and set aditional macros
  * @author Pavel Železný <*****@*****.**>
  * @param \Nette\Templating\Template $template
  * @return void
  */
 public function templatePrepareFilters($template)
 {
     $template->registerFilter($latte = new \Nette\Latte\Engine());
     $set = \Nette\Latte\Macros\MacroSet::install($latte->getCompiler());
     $set->addMacro('src', NULL, NULL, 'echo \'src="\'.\\Nette\\Templating\\Helpers::dataStream(file_get_contents(%node.word)).\'"\'');
 }
 /**
  * Load latte and set aditional macros
  * @author Pavel Železný <*****@*****.**>
  * @param \Nette\Templating\Template $template
  * @return void
  */
 public function templatePrepareFilters($template)
 {
     $template->registerFilter($latte = new \Nette\Latte\Engine());
     $set = new \Nette\Latte\Macros\MacroSet($latte->getCompiler());
     $set->addMacro('src', NULL, NULL, 'echo \'src="\'.\\Nette\\Templating\\Helpers::dataStream(file_get_contents(%node.word)).\'"\'');
     $set->addMacro('stylesheet', 'echo \'<style type="text/css">\'.file_get_contents(%node.word).\'</style>\'');
     if (class_exists('Tracy\\Dumper')) {
         $dumperMethod = '\\Tracy\\Dumper::toHtml';
     } else {
         $dumperMethod = '\\Nette\\Diagnostics\\Helpers::clickableDump';
     }
     $set->addMacro('clickableDump', 'echo ' . $dumperMethod . '(%node.word)');
 }