Skip to content

Instantly share code, notes, and snippets.

@LeonardoCA
Created November 9, 2012 18:12
Show Gist options
  • Save LeonardoCA/4047257 to your computer and use it in GitHub Desktop.
Save LeonardoCA/4047257 to your computer and use it in GitHub Desktop.
bootstrap renderer partial optimalization
/**
* @param \Nette\Templating\FileTemplate $template
*/
public function __construct(FileTemplate $template = NULL)
{
if ($template === NULL) {
$template = new FileTemplate();
$template->registerFilter(new Nette\Latte\Engine());
} else {
$template->setParameters(array_fill_keys(array(
'control', '_control', 'presenter', '_presenter'
), NULL));
}
$this->template = $template;
$this->template->setFile(__DIR__ . '/@all.latte');
$this->template->renderer = $this;
}
/**
* 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)
{
//sdump(array($form, $mode, $args, is_object($mode) ? $mode->htmlId : ''));
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->form = $this->form;
$this->template->_form = $this->form;
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 {
$this->template->mode = $mode;
$this->template->render();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment