Skip to content

Instantly share code, notes, and snippets.

@Olicek
Created March 21, 2017 11:28
Show Gist options
  • Save Olicek/e6ba39801418fc304368c23b9ff607ac to your computer and use it in GitHub Desktop.
Save Olicek/e6ba39801418fc304368c23b9ff607ac to your computer and use it in GitHub Desktop.
Materialize renderer
/**
* Class AdminRenderer
*/
final class AdminRenderer extends DefaultFormRenderer implements IFormRenderer
{
/**
* AdminRenderer constructor.
*/
public function __construct()
{
$this->wrappers['error']['container'] = 'div class="card-panel teal red"';
$this->wrappers['error']['item'] = 'div';
$this->wrappers['controls']['container'] = 'div class="col s12"';
$this->wrappers['pair']['container'] = 'div class="row"';
$this->wrappers['pair']['.error'] = 'has-error';
$this->wrappers['control']['container'] = 'div class="input-field col s12"';
$this->wrappers['label']['container'] = '';
$this->wrappers['label']['requiredsuffix'] = Html::el('span', '*')
->addAttributes(['class' => 'blue-text text-red']);
$this->wrappers['control']['description'] = 'span class=help-block';
$this->wrappers['control']['errorcontainer'] = 'span class=help-block';
} // __construct()
/**
* Renders single visual row.
* @param IControl $control
* @return string
*/
public function renderPair(IControl $control) : string
{
$pair = $this->getWrapper('pair container');
if($control instanceof RadioList)
{
$control->generateId = true;
$pair->addHtml($this->renderLabel($control)->addHtml($this->renderControl($control)));
}
else {
$pair->addHtml($this->renderControl($control)->addHtml($this->renderLabel($control)));
}
$pair->class($this->getValue($control->isRequired() ? 'pair .required' : 'pair .optional'), true);
$pair->class($control->hasErrors() ? $this->getValue('pair .error') : null, true);
$pair->class($control->getOption('class'), true);
if(++$this->counter % 2)
{
$pair->class($this->getValue('pair .odd'), true);
}
$pair->id = $control->getOption('id');
return $pair->render(0);
}
/**
* Renders 'label' part of visual row of controls.
* @param IControl $control
* @return Html
*/
public function renderLabel(Nette\Forms\IControl $control)
{
$suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : '');
if($control instanceof Nette\Forms\Controls\Checkbox)
{
$label = $control->getLabelPart();
}
else {
$label = $control->getLabel();
}
if($label instanceof Html)
{
$label->addHtml($suffix);
if($control->isRequired())
{
$label->class($this->getValue('control .required'), true);
}
}
elseif($label != null) // @intentionally ==
{
$label .= $suffix;
}
return $this->getWrapper('label container')->setHtml($label);
}
/**
* Renders 'control' part of visual row of controls.
* @param IControl $control
* @return Html
*/
public function renderControl(Nette\Forms\IControl $control) : Html
{
$body = $this->getWrapper('control container');
if($this->counter % 2)
{
$body->class($this->getValue('control .odd'), true);
}
$description = $control->getOption('description');
if($description instanceof Html)
{
$description = ' ' . $description;
}
elseif(is_string($description))
{
if($control instanceof Nette\Forms\Controls\BaseControl)
{
$description = $control->translate($description);
}
$description = ' ' . $this->getWrapper('control description')->setText($description);
}
else {
$description = '';
}
if($control->isRequired())
{
$description = $this->getValue('control requiredsuffix') . $description;
}
$control->setOption('rendered', true);
if($control instanceof Nette\Forms\Controls\Checkbox)
{
$element = $control->getControlPart();
}
else {
$element = $control->getControl();
}
if($element instanceof Html && $element->getName() === 'input')
{
$element->class($this->getValue("control .$element->type"), true);
}
return $body->setHtml($element . $description . $this->renderErrors($control));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment