Created
March 28, 2010 21:30
-
-
Save janmarek/347052 to your computer and use it in GitHub Desktop.
Form macros
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 | |
// blabla... | |
Nette\Templates\FormMacros::register(); | |
// blabla... |
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 Nette\Templates; | |
use Nette\Forms\Form; | |
use Nette\String; | |
/** | |
* Form macros | |
* | |
* @author Jan Marek | |
* @license MIT | |
*/ | |
class FormMacros extends \Nette\Object { | |
private static $form; | |
public function __construct() { | |
throw new \InvalidStateException("Static class."); | |
} | |
public static function register() { | |
LatteMacros::$defaultMacros["form"] = '<?php %Nette\Templates\FormMacros::macroBegin% ?>'; | |
LatteMacros::$defaultMacros["input"] = '<?php %Nette\Templates\FormMacros::macroInput% ?>'; | |
LatteMacros::$defaultMacros["label"] = '<?php %Nette\Templates\FormMacros::macroLabel% ?>'; | |
LatteMacros::$defaultMacros["/form"] = '<?php Nette\Templates\FormMacros::end() ?>'; | |
} | |
public static function macroBegin($content) { | |
list($name, $modifiers) = self::fetchNameAndModifiers($content); | |
return "\$formErrors = Nette\Templates\FormMacros::begin($name, \$control, $modifiers)->getErrors()"; | |
} | |
public static function begin($form, $control, $modifiers = array()) { | |
if ($form instanceof Form) { | |
self::$form = $form; | |
} else { | |
self::$form = $control[$form]; | |
} | |
if (isset($modifiers["class"])) { | |
self::$form->getElementPrototype()->class[] = $modifiers["class"]; | |
} | |
self::$form->render("begin"); | |
return self::$form; | |
} | |
public static function end() { | |
self::$form->render("end"); | |
} | |
public static function macroInput($content) { | |
list($name, $modifiers) = self::fetchNameAndModifiers($content); | |
return "Nette\Templates\FormMacros::input($name, $modifiers)"; | |
} | |
public static function input($name, $modifiers = array()) { | |
$input = self::$form[$name]->getControl(); | |
if (isset($modifiers["size"])) { | |
$input->size($modifiers["size"]); | |
} | |
if (isset($modifiers["rows"])) { | |
$input->rows($modifiers["rows"]); | |
} | |
if (isset($modifiers["cols"])) { | |
$input->cols($modifiers["cols"]); | |
} | |
if (isset($modifiers["class"])) { | |
$input->class[] = $modifiers["class"]; | |
} | |
if (isset($modifiers["style"])) { | |
$input->style($modifiers["style"]); | |
} | |
if (isset($modifiers["value"])) { | |
$input->value($modifiers["value"]); | |
} | |
echo $input; | |
} | |
public static function macroLabel($content) { | |
list($name, $modifiers) = self::fetchNameAndModifiers($content); | |
return "Nette\Templates\FormMacros::label($name, $modifiers)"; | |
} | |
public static function label($name, $modifiers = array()) { | |
$label = self::$form[$name]->getLabel(); | |
if (isset($modifiers["text"])) { | |
$label->setText($modifiers["text"]); | |
} | |
if (isset($modifiers["class"])) { | |
$label->class[] = $modifiers["class"]; | |
} | |
if (isset($modifiers["style"])) { | |
$label->style($modifiers["style"]); | |
} | |
echo $label; | |
} | |
// helper | |
private static function fetchNameAndModifiers($code) { | |
$name = LatteFilter::fetchToken($code); | |
$modifiers = LatteFilter::formatArray($code); | |
$name = String::startsWith($name, '$') ? $name : "'$name'"; | |
$modifiers = $modifiers ?: "array()"; | |
return array($name, $modifiers); | |
} | |
} |
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
{form nazevFormu} | |
<p class="error" n:foreach="$formErrors as $error">{$error}</p> | |
<p>{label name class => "big", text => "Jméno:"} {input name size => 30}</p> | |
<p>{input ok text => "Odeslat formulář"}</p> | |
{/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 | |
class TestPresenter extends BasePresenter { | |
protected function createComponentNazevFormu() { | |
$form = new Nette\Application\AppForm; | |
$form->addText("name"); | |
$form->addSubmit("ok"); | |
return $form; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rozšírenie o podporu container-ov