Skip to content

Commit 9b3ccf1

Browse files
committed
Latte: split initialization and form rendering
allows <form n:name n:tag-if=false>
1 parent d091742 commit 9b3ccf1

File tree

13 files changed

+49
-12
lines changed

13 files changed

+49
-12
lines changed

src/Bridges/FormsLatte/FormMacros.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
6767
$node->replaced = true;
6868
$node->tokenizer->reset();
6969
return $writer->write(
70-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = '
70+
'$form = $this->global->formsStack[] = '
7171
. ($name[0] === '$'
7272
? 'is_object($ʟ_tmp = %node.word) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]'
7373
: '$this->global->uiControl[%node.word]')
74-
. ', %node.array)'
74+
. ';'
75+
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
76+
. 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node.array)'
7577
. " /* line $node->startLine */;"
7678
);
7779
}
@@ -226,7 +228,8 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
226228
. ($name[0] === '$'
227229
? 'is_object($ʟ_tmp = %0.word) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]'
228230
: '$this->global->uiControl[%0.word]')
229-
. " /* line $node->startLine */; ?>",
231+
. " /* line $node->startLine */;"
232+
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form); ?>',
230233
$name
231234
);
232235
return $writer->write(

src/Bridges/FormsLatte/Nodes/FormNNameNode.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function print(PrintContext $context): string
4747
? '$this->global->uiControl[%node]'
4848
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
4949
. ' %line;'
50+
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
5051
. '%node '
5152
. 'array_pop($this->global->formsStack);',
5253
$this->name,

src/Bridges/FormsLatte/Nodes/FormNode.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function print(PrintContext $context): string
6666
? '$this->global->uiControl[%node]'
6767
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
6868
. ' %line;'
69+
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
6970
. ($this->print
7071
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node) %1.line;'
7172
: '')

src/Bridges/FormsLatte/Runtime.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ class Runtime
2323
{
2424
use Nette\StaticClass;
2525

26-
/**
27-
* Renders form begin.
28-
*/
29-
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
26+
public static function initializeForm(Form $form): void
3027
{
3128
$form->fireRenderEvents();
3229
foreach ($form->getControls() as $control) {
3330
$control->setOption('rendered', false);
3431
}
32+
}
33+
3534

35+
/**
36+
* Renders form begin.
37+
*/
38+
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
39+
{
3640
$el = $form->getElementPrototype();
3741
$el->action = (string) $el->action;
3842
$el = clone $el;

tests/Forms.Latte2/expected/FormMacros.button.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
%A%
33
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 1 */;
4+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
45
echo '<form';
56
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
67
echo '>

tests/Forms.Latte2/expected/FormMacros.formContainer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
%A%
3-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */;
3+
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
4+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
5+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 1 */;
46
echo '
57
<table>
68
<tr>

tests/Forms.Latte2/expected/FormMacros.forms.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
%A%
3-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], ['id' => 'myForm', 'class'=>"ajax"]) /* line 1 */;
3+
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
4+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
5+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, ['id' => 'myForm', 'class'=>"ajax"]) /* line 1 */;
46
echo "\n";
57
$iterations = 0;
68
foreach (['id', 'username', 'select', 'area', 'send'] as $name) /* line 2 */ {
@@ -76,12 +78,16 @@
7678
7779
7880
';
79-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 27 */;
81+
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
82+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
83+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 27 */;
8084
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
8185
echo '
8286
8387
';
84-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 29 */;
88+
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
89+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
90+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 29 */;
8591
echo "\n";
8692
$iterations = 0;
8793
foreach ($form['sex']->items as $key => $label) /* line 31 */ {
@@ -197,6 +203,7 @@
197203
198204
';
199205
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 58 */;
206+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
200207
if (1) /* line 58 */ {
201208
echo '<form id="myForm" class="ajax"';
202209
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['id' => null, 'class' => null], false);
@@ -214,6 +221,7 @@
214221
215222
';
216223
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 63 */;
224+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
217225
echo '<form';
218226
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line 63 */;
219227
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['class' => null], false);
@@ -230,6 +238,7 @@
230238
231239
';
232240
$form = $this->global->formsStack[] = is_object($ʟ_tmp = $this->global->uiControl['myForm']) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp] /* line 68 */;
241+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
233242
echo '<FORM';
234243
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
235244
echo '>

tests/Forms.Latte2/expected/FormMacros.get.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?php
22
%A%
3-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */;
3+
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
4+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
5+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 1 */;
46
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
57
echo '
68
79
';
810
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 3 */;
11+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
912
echo '<form';
1013
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
1114
echo '>

tests/Forms.Latte3/expected/forms.button.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
%A%
33
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
4+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
45
echo '<form';
56
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
67
echo '>

tests/Forms.Latte3/expected/forms.formContainer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
%A%
33
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
4+
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
45
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */;
56
echo '
67
<table>

0 commit comments

Comments
 (0)