Skip to content

Commit bdfb7de

Browse files
committed
FormMacros: added {formPrint $form}
1 parent fd5d69d commit bdfb7de

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/Bridges/FormsLatte/FormMacros.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Latte\MacroNode;
1515
use Latte\Macros\MacroSet;
1616
use Latte\PhpWriter;
17+
use Nette\Forms;
1718

1819

1920
/**
@@ -36,6 +37,7 @@ public static function install(Latte\Compiler $compiler): void
3637
$me->addMacro('input', [$me, 'macroInput']);
3738
$me->addMacro('name', [$me, 'macroName'], [$me, 'macroNameEnd'], [$me, 'macroNameAttr']);
3839
$me->addMacro('inputError', [$me, 'macroInputError']);
40+
$me->addMacro('formPrint', [$me, 'macroFormPrint']);
3941
}
4042

4143

@@ -240,4 +242,57 @@ public function macroInputError(MacroNode $node, PhpWriter $writer)
240242
return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError());', $name);
241243
}
242244
}
245+
246+
247+
/**
248+
* {formPrint [ClassName]}
249+
*/
250+
public function macroFormPrint(MacroNode $node, PhpWriter $writer)
251+
{
252+
$name = $node->tokenizer->fetchWord();
253+
if ($name == null) { // null or false
254+
throw new CompileException('Missing form name in ' . $node->getNotation());
255+
}
256+
$node->tokenizer->reset();
257+
return $writer->write(
258+
__CLASS__ . '::macroFromPrintRuntime('
259+
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
260+
. '$this->global->uiControl[%node.word]);'
261+
);
262+
}
263+
264+
265+
/**
266+
* Generates blueprint of form.
267+
*/
268+
public static function macroFromPrintRuntime($form): void
269+
{
270+
$dummy = new Forms\Form;
271+
foreach ($form->getControls() as $name => $input) {
272+
if ($input instanceof Forms\Controls\Hidden) {
273+
continue;
274+
}
275+
$dummyControl = new class extends Forms\Controls\BaseControl {
276+
public function getLabel($name = null)
277+
{
278+
return '{label ' . $this->getName() . '}';
279+
}
280+
281+
282+
public function getControl()
283+
{
284+
return '{input ' . $this->getName() . '}';
285+
}
286+
};
287+
$dummy->addComponent($dummyControl->setRequired($input->isRequired()), (string) $name);
288+
}
289+
$dummy->setRenderer($form->getRenderer());
290+
291+
ob_end_clean();
292+
header('Content-Type: text/plain');
293+
echo '<form n:name="' . $form->getName() . '">';
294+
echo $dummy->render('body');
295+
echo '</form>';
296+
exit;
297+
}
243298
}

0 commit comments

Comments
 (0)