Skip to content

Instantly share code, notes, and snippets.

@JanTvrdik
Forked from janmarek/FormMacros.php
Created June 12, 2010 10:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanTvrdik/435631 to your computer and use it in GitHub Desktop.
Save JanTvrdik/435631 to your computer and use it in GitHub Desktop.
Macros for manual form rendering in Nette Framework
<?php
// blabla...
Nette\Templates\FormMacros::register();
// blabla...
<?php
namespace Nette\Templates;
use Nette;
use Nette\String;
use Nette\Forms\Form;
/**
* Form macros
*
* @author Jan Marek, Jan Tvrdík
* @license MIT
*/
class FormMacros extends Nette\Object
{
/** @var Nette\Forms\Form */
private static $form;
/** @var int */
private static $containerLevel = 0;
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() ?>';
LatteMacros::$defaultMacros["formContainer"] = '<?php %Nette\Templates\FormMacros::macroBeginContainer% ?>';
LatteMacros::$defaultMacros["/formContainer"] = '<?php Nette\Templates\FormMacros::endContainer() ?>';
}
public static function macroBegin($content)
{
list($name, $modifiers) = self::fetchNameAndModifiers($content);
return "\$formErrors = Nette\Templates\FormMacros::begin($name, \$control, $modifiers)->getErrors()";
}
public static function macroBeginContainer($content)
{
$name = LatteFilter::formatString(LatteFilter::fetchToken($content));
return 'Nette\Templates\FormMacros::beginContainer(' . $name . ')';
}
public static function beginContainer($name)
{
$container = self::$form[$name];
if (!$container instanceof Nette\Forms\FormContainer) {
throw new \InvalidArgumentException();
}
self::$form = $container;
self::$containerLevel++;
}
public static function endContainer()
{
if (self::$containerLevel < 1) {
throw new \InvalidStateException('Trying to close container which is not open.');
}
self::$form = self::$form->getParent();
self::$containerLevel--;
}
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()
{
if (self::$containerLevel > 0) {
throw new \InvalidStateException('There are some unclosed containers.');
}
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"]);
}
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 = LatteFilter::formatString($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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment