Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Forked from janmarek/FormMacros.php
Created October 3, 2010 11:03
Show Gist options
  • Save Mikulas/608501 to your computer and use it in GitHub Desktop.
Save Mikulas/608501 to your computer and use it in GitHub Desktop.
Form Macros
<?php
Nette\Templating\FormMacros::register();
<?php
namespace Nette\Templating;
use Nette\Forms\Form;
use Nette\Utils\Strings as String;
use Nette\InvalidStateException;
use Nette\Object;
use Nette\Latte\DefaultMacros;
/**
* Form macros
*
* @author Jan Marek, Mikuláš Dítě
* @license MIT
*/
class FormMacros extends Object
{
private static $form;
public function __construct()
{
throw new InvalidStateException("Static class.");
}
public static function register()
{
DefaultMacros::$defaultMacros["form"] = '<?php %' . __CLASS__ . '::macroBegin% ?>';
DefaultMacros::$defaultMacros["input"] = '<?php %' . __CLASS__ . '::macroInput% ?>';
DefaultMacros::$defaultMacros["label"] = '<?php %' . __CLASS__ . '::macroLabel% ?>';
DefaultMacros::$defaultMacros["/form"] = '<?php ' . __CLASS__ . '::end() ?>';
}
public static function macroBegin($content)
{
list($name, $modifiers) = self::fetchNameAndModifiers($content);
return "\$formErrors = " . __CLASS__ . "::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 __CLASS__ . "::input($name, $modifiers)";
}
public static function input($name, $modifiers = array())
{
$input = self::$form[$name]->getControl();
foreach ($modifiers as $attribute => $value) {
if (strCaseCmp($attribute, 'class') === 0) {
$input->class[] = $value;
} else {
$input->$attribute($value);
}
}
echo $input;
}
public static function macroLabel($content)
{
list($name, $modifiers) = self::fetchNameAndModifiers($content);
return __CLASS__ . "::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;
}
private static function fetchNameAndModifiers($code)
{
$name = DefaultMacros::fetchToken($code);
$macros = new DefaultMacros;
$modifiers = $macros->formatArray($code);
$name = String::startsWith($name, '$') ? $name : "'$name'";
$modifiers = $modifiers ?: "array()";
return array($name, $modifiers);
}
}
Template example:
{form Form}
<p class="error" n:foreach="$formErrors as $error">{$error}</p>
<p>{label name class => "big", text => "Jméno:"}&nbsp;{input name size => 30}</p>
<p>{input save text => "Odeslat formulář"}</p>
{/form}
Errors inside snippet:
{form signInForm}
{snippet errors}
{control signInForm errors}
{/snippet}
<p>{label username, text => "User:"}&nbsp;{input username size => 30}</p>
<p>{label password, text => "Password:"}&nbsp;{input password size => 30}</p>
<p>{input send, value => "Login"}</p>
{/form}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment