1414use Latte \MacroNode ;
1515use Latte \Macros \MacroSet ;
1616use 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,73 @@ 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__ . '::macroFormPrintRuntime( '
259+ . ($ name [0 ] === '$ ' ? 'is_object(%node.word) ? %node.word : ' : '' )
260+ . '$this->global->uiControl[%node.word]); exit; '
261+ );
262+ }
263+
264+
265+ /**
266+ * Generates blueprint of form.
267+ */
268+ public static function macroFormPrintRuntime ($ form ): void
269+ {
270+ $ dummy = new Forms \Form ;
271+ foreach ($ form ->getControls () as $ name => $ input ) {
272+ $ dummyControl = new class extends Forms \Controls \BaseControl {
273+ public $ inner ;
274+
275+
276+ public function getLabel ($ name = null )
277+ {
278+ return $ this ->inner ->getLabel () ? '{label ' . $ this ->getName () . '} ' : null ;
279+ }
280+
281+
282+ public function getControl ()
283+ {
284+ return '{input ' . $ this ->getName () . '} ' ;
285+ }
286+
287+
288+ public function isRequired (): bool
289+ {
290+ return $ this ->inner ->isRequired ();
291+ }
292+
293+
294+ public function getOption ($ key , $ default = null )
295+ {
296+ return $ key === 'rendered ' ? parent ::getOption ($ key ) : $ this ->inner ->getOption ($ key , $ default );
297+ }
298+ };
299+ $ dummyControl ->inner = $ input ;
300+ $ dummy ->addComponent ($ dummyControl , (string ) $ name );
301+ }
302+
303+ $ dummy ->setRenderer ($ form ->getRenderer ());
304+ ob_start ();
305+ $ dummy ->render ('body ' );
306+ $ body = ob_get_clean ();
307+
308+ $ blueprint = new Latte \Runtime \Blueprint ;
309+ $ end = $ blueprint ->printCanvas ();
310+ $ blueprint ->printHeader ('Form ' . $ form ->getName ());
311+ $ blueprint ->printCode ('<form n:name=" ' . $ form ->getName () . '"> ' . $ body . '</form> ' );
312+ echo $ end ;
313+ }
243314}
0 commit comments