Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Form macros
<?php
// blabla...
Nette\Templates\FormMacros::register();
// blabla...
<?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);
}
}
{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}
<?php
class TestPresenter extends BasePresenter {
protected function createComponentNazevFormu() {
$form = new Nette\Application\AppForm;
$form->addText("name");
$form->addSubmit("ok");
return $form;
}
}

sjurco commented May 24, 2011

Rozšírenie o podporu container-ov

NLatteMacros::$defaultMacros["container"] = '<?php Nette\Templates\FormMacros::containerBegin(%%) ?>';
NLatteMacros::$defaultMacros["/container"] = '<?php Nette\Templates\FormMacros::containerEnd() ?>';

public static function containerBegin($name) {
    self::$form = self::$form[$name];
}

public static function containerEnd() {
    self::$form = self::$form->getParent();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment