Skip to content

Instantly share code, notes, and snippets.

@ViktorStiskala
Created August 28, 2010 19:53
Show Gist options
  • Save ViktorStiskala/555502 to your computer and use it in GitHub Desktop.
Save ViktorStiskala/555502 to your computer and use it in GitHub Desktop.
Custom renderer for Nette framework with containerClass support
<?php
/**
* Extended form renderer.
*
* @author Viktor Stískala <viktor(at)stiskala.cz>
*/
class CustomRenderer extends ConventionalRenderer
{
/**
* Renders 'control' part of visual row of controls.
* added containerClass option support. If this option is not set it uses parent::renderControl
* @param IFormControl
* @return string
*/
public function renderControl(IFormControl $control)
{
// use parent renderControl when no option was set
if(!$control->getOption('containerClass'))
return parent::renderControl($control);
$body = $this->getWrapper('control container');
if($control->getOption('containerClass'))
$body->class = $control->getOption('containerClass');
if ($this->counter % 2) $body->class($this->getValue('control .odd'), TRUE);
$description = $control->getOption('description');
if ($description instanceof Html) {
$description = ' ' . $control->getOption('description');
} elseif (is_string($description)) {
$description = ' ' . $this->getWrapper('control description')->setText($control->translate($description));
} else {
$description = '';
}
if ($control->getOption('required')) {
$description = $this->getValue('control requiredsuffix') . $description;
}
if ($this->getValue('control errors')) {
$description .= $this->renderErrors($control);
}
if ($control instanceof Checkbox || $control instanceof Button) {
return $body->setHtml((string) $control->getControl() . (string) $control->getLabel() . $description);
} else {
return $body->setHtml((string) $control->getControl() . $description);
}
}
/**
* Renders single visual row of multiple controls.
* added containerClass support
* @param array of IFormControl
* @return string
*/
public function renderPairMulti(array $controls)
{
$s = array();
$containerClass = null;
foreach ($controls as $control) {
if (!($control instanceof IFormControl)) {
throw new InvalidArgumentException("Argument must be array of IFormControl instances.");
}
$s[] = (string) $control->getControl();
if($control->getOption('containerClass'))
$containerClass = $control->getOption('containerClass');
}
$pair = $this->getWrapper('pair container');
if($containerClass)
$pair->class = $containerClass;
$pair->add($this->renderLabel($control));
$pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s)));
return $pair->render(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment