Skip to content

Instantly share code, notes, and snippets.

@hrach
Created March 10, 2012 00:20
Show Gist options
  • Save hrach/2009432 to your computer and use it in GitHub Desktop.
Save hrach/2009432 to your computer and use it in GitHub Desktop.
Signaly Form macros
<?php
/**
* @license MIT license
*/
namespace Nextras;
use Nette,
Nette\Latte,
Nette\Latte\MacroNode,
Nette\Latte\CompileException,
Nette\Utils\Strings;
/**
* Macros for Nette\Forms.
*
* - {input name}
*/
class FormMacros extends Latte\Macros\MacroSet
{
/** @var array */
public static $inputTypeClasses = array(
'.required' => 'required',
'.text' => 'text',
'.password' => 'text',
'.file' => 'text',
'.submit' => 'button',
'.image' => 'imagebutton',
'.button' => 'button',
);
/**
*/
public static function install(Latte\Compiler $compiler)
{
$me = new static($compiler);
$me->addMacro('label', array($me, 'macroLabel'), '?></label><?php');
$me->addMacro('input', 'echo \Nextras\FormMacros::inputControl($_form[%node.word], %node.array)', NULL, array($me, 'macroAttrInput'));
$me->addMacro('form',
'Nette\Latte\Macros\FormMacros::renderFormBegin($form = $_form = is_object(%node.word) ? %node.word : $_control[%node.word], %node.array)',
'Nette\Latte\Macros\FormMacros::renderFormEnd($_form)'
);
$me->addMacro('formErrors', 'foreach ($_form->getErrors() as $error) { echo "<p class=\"message-error\">" . Nette\Templating\Helpers::escapeHtml($error, ENT_NOQUOTES) . "</p>"; }');
}
/**
* {label ...} and optionally {/label}
*/
public function macroLabel(MacroNode $node, $writer)
{
if ($node->isEmpty = (substr($node->args, -1) === '/')) {
$node->setArgs(substr($node->args, 0, -1));
return
$writer->write('if ($_label = $_form[%node.word]->getLabel()) {
$_attrs = %node.array; if (isset($_attrs["class"])) { $_label->class[] = $_attrs["class"]; unset($_attrs["class"]); } $_label->addAttributes($_attrs);
$_labelC = $_form[%node.word]; if (!$_labelC->getOption("required")) {
$_label->class[] = "optional";
if (isset($_attrs["optional"]) && $_attrs["optional"]) $_label->setHtml(Nette\Templating\Helpers::escapeHtml($_label->getText(), ENT_NOQUOTES) . "<small>nepovinné</small>");
} echo $_label;
}');
} else {
return $writer->write('if ($_label = $_form[%node.word]->getLabel()) echo $_label->addAttributes(%node.array)->startTag()');
}
}
/**
* n:input
*/
public function macroAttrInput(MacroNode $node, $writer)
{
$control = get_called_class() . '::inputControl($_form[%node.word])';
if ($node->htmlNode->attrs) {
$reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
return $writer->write('echo ' . $control . '->addAttributes(%var)->attributes()', $reset);
}
return $writer->write('echo ' . $control . '->attributes()');
}
/**
* @param \Nette\Forms\IControl $control
* @return \Nette\Utils\Html
*/
public static function inputControl(Nette\Forms\IControl $control, $attrs)
{
/** @var Nette\Utils\Html */
$el = $control->getControl();
foreach ($attrs as $key => $val) $el->{"add$key"}($val);
if ($el->getName() === 'input' && isset(static::$inputTypeClasses['.' . $el->type])) {
$el->addClass(static::$inputTypeClasses['.' . $el->type]);
}
return $el;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment