From bdfb7de61721e8aa7afbc15408c91039ff9eaa5d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 10 Jan 2020 12:10:45 +0100 Subject: [PATCH] FormMacros: added {formPrint $form} --- src/Bridges/FormsLatte/FormMacros.php | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/Bridges/FormsLatte/FormMacros.php b/src/Bridges/FormsLatte/FormMacros.php index 4ef32f3cc..4a92f7dd8 100644 --- a/src/Bridges/FormsLatte/FormMacros.php +++ b/src/Bridges/FormsLatte/FormMacros.php @@ -14,6 +14,7 @@ use Latte\MacroNode; use Latte\Macros\MacroSet; use Latte\PhpWriter; +use Nette\Forms; /** @@ -36,6 +37,7 @@ public static function install(Latte\Compiler $compiler): void $me->addMacro('input', [$me, 'macroInput']); $me->addMacro('name', [$me, 'macroName'], [$me, 'macroNameEnd'], [$me, 'macroNameAttr']); $me->addMacro('inputError', [$me, 'macroInputError']); + $me->addMacro('formPrint', [$me, 'macroFormPrint']); } @@ -240,4 +242,57 @@ public function macroInputError(MacroNode $node, PhpWriter $writer) return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError());', $name); } } + + + /** + * {formPrint [ClassName]} + */ + public function macroFormPrint(MacroNode $node, PhpWriter $writer) + { + $name = $node->tokenizer->fetchWord(); + if ($name == null) { // null or false + throw new CompileException('Missing form name in ' . $node->getNotation()); + } + $node->tokenizer->reset(); + return $writer->write( + __CLASS__ . '::macroFromPrintRuntime(' + . ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '') + . '$this->global->uiControl[%node.word]);' + ); + } + + + /** + * Generates blueprint of form. + */ + public static function macroFromPrintRuntime($form): void + { + $dummy = new Forms\Form; + foreach ($form->getControls() as $name => $input) { + if ($input instanceof Forms\Controls\Hidden) { + continue; + } + $dummyControl = new class extends Forms\Controls\BaseControl { + public function getLabel($name = null) + { + return '{label ' . $this->getName() . '}'; + } + + + public function getControl() + { + return '{input ' . $this->getName() . '}'; + } + }; + $dummy->addComponent($dummyControl->setRequired($input->isRequired()), (string) $name); + } + $dummy->setRenderer($form->getRenderer()); + + ob_end_clean(); + header('Content-Type: text/plain'); + echo '
'; + echo $dummy->render('body'); + echo '
'; + exit; + } }