Created
July 18, 2011 21:08
-
-
Save JanVoracek/1090660 to your computer and use it in GitHub Desktop.
Nette Form ScaffoldingRenderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<textarea cols="150" rows="30">{!$form}</textarea> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \Nette\Application\UI\Form; | |
class FooPresenter extends BasePresenter | |
{ | |
public function renderDefault($id=null) | |
{ | |
$this['fooForm']->setRenderer(new \JanVoracek\Forms\Rendering\LatteScaffoldingRenderer()); | |
$this->template->form = $this['fooForm']; | |
} | |
protected function createComponentFooForm($name) | |
{ | |
$form = new Form($this, $name); | |
$form->addText('foo','Foo:'); | |
$form->addSubmit('send'); | |
return $form; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace JanVoracek\Forms\Rendering; | |
/** | |
* Scaffolding Renderer. | |
* | |
* Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com) | |
* Copyright (c) 2011 Jan Voráček | |
* | |
* This source file is subject to the New BSD License. | |
* For more information please see http://addons.nettephp.com/scaffoldingrenderer | |
* | |
* @package Nette Addons | |
*/ | |
use \Nette\Utils\Html, | |
\Nette\Forms\Rendering\DefaultFormRenderer, | |
\Nette\Forms\IControl, | |
\Nette\Forms\Controls\Checkbox, | |
\Nette\Forms\Controls\Button; | |
/** | |
* Builds a latte template. | |
* | |
* @author David Grudl, Jan Voráček | |
* @copyright Copyright (c) 2008, 2009 David Grudl, 2011 Jan Voráček | |
* @package Nette Extras | |
*/ | |
class LatteScaffoldingRenderer extends DefaultFormRenderer | |
{ | |
/** | |
* Renders form begin. | |
* @return string | |
*/ | |
public function renderBegin() | |
{ | |
return "{form ".$this->form->name."}"; | |
} | |
/** | |
* Renders form end. | |
* @return string | |
*/ | |
public function renderEnd() | |
{ | |
return "{/form}"; | |
} | |
/** | |
* Renders validation errors (per form or per control). | |
* @param IControl | |
* @return void | |
*/ | |
public function renderErrors(IControl $control = NULL) | |
{ | |
return "{control \$form errors}"; // no macro for form errors | |
} | |
/** | |
* Renders single visual row of multiple controls. | |
* @param array of IControl | |
* @return string | |
*/ | |
public function renderPairMulti(array $controls) | |
{ | |
$s = array(); | |
foreach ($controls as $control) { | |
if (!($control instanceof IControl)) { | |
throw new InvalidArgumentException("Argument must be array of IControl instances."); | |
} | |
$control->setOption('rendered', TRUE); | |
$name = $control->lookupPath('Nette\Forms\Form'); | |
$s[] = "{input $name}"; | |
} | |
$pair = $this->getWrapper('pair container'); | |
$pair->add($this->getWrapper('label container')->setHtml(' ')); | |
$pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s))); | |
return $pair->render(0); | |
} | |
/** | |
* Renders 'label' part of visual row of controls. | |
* @param IControl | |
* @return string | |
*/ | |
public function renderLabel(IControl $control) | |
{ | |
$head = $this->getWrapper('label container'); | |
if ($control instanceof Checkbox || $control instanceof Button) { | |
return $head->setHtml(' '); | |
} else { | |
$name = $control->lookupPath('Nette\Forms\Form'); | |
return $head->setHtml("{label $name /}" . $this->getValue('label suffix')); | |
} | |
} | |
/** | |
* Renders 'control' part of visual row of controls. | |
* @param IControl | |
* @return string | |
*/ | |
public function renderControl(IControl $control) | |
{ | |
$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 = ' ' . $control->getOption('description'); | |
} elseif (is_string($description)) { | |
$description = ' ' . $this->getWrapper('control description')->setText($description); | |
} else { | |
$description = ''; | |
} | |
if ($this->getValue('control errors')) { | |
$description .= $this->renderErrors($control); | |
} | |
$control->setOption('rendered', TRUE); | |
$name = $control->lookupPath('Nette\Forms\Form'); | |
if ($control instanceof Checkbox || $control instanceof Button) { | |
return $body->setHtml("{input $name}{label \$form['$name'] /}" . $description); | |
} else { | |
return $body->setHtml("{input $name}" . $description); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment