Skip to content

Commit

Permalink
Latte: split initialization and form rendering
Browse files Browse the repository at this point in the history
allows <form n:name n:tag-if=false>
  • Loading branch information
dg committed Oct 9, 2023
1 parent d091742 commit 9b3ccf1
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 12 deletions.
9 changes: 6 additions & 3 deletions src/Bridges/FormsLatte/FormMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
$node->replaced = true;
$node->tokenizer->reset();
return $writer->write(
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = '
'$form = $this->global->formsStack[] = '
. ($name[0] === '$'
? 'is_object($ʟ_tmp = %node.word) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]'
: '$this->global->uiControl[%node.word]')
. ', %node.array)'
. ';'
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
. 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node.array)'
. " /* line $node->startLine */;"
);
}
Expand Down Expand Up @@ -226,7 +228,8 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
. ($name[0] === '$'
? 'is_object($ʟ_tmp = %0.word) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]'
: '$this->global->uiControl[%0.word]')
. " /* line $node->startLine */; ?>",
. " /* line $node->startLine */;"
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form); ?>',
$name
);
return $writer->write(
Expand Down
1 change: 1 addition & 0 deletions src/Bridges/FormsLatte/Nodes/FormNNameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function print(PrintContext $context): string
? '$this->global->uiControl[%node]'
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
. ' %line;'
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
. '%node '
. 'array_pop($this->global->formsStack);',
$this->name,
Expand Down
1 change: 1 addition & 0 deletions src/Bridges/FormsLatte/Nodes/FormNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function print(PrintContext $context): string
? '$this->global->uiControl[%node]'
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
. ' %line;'
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
. ($this->print
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node) %1.line;'
: '')
Expand Down
12 changes: 8 additions & 4 deletions src/Bridges/FormsLatte/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ class Runtime
{
use Nette\StaticClass;

/**
* Renders form begin.
*/
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
public static function initializeForm(Form $form): void
{
$form->fireRenderEvents();
foreach ($form->getControls() as $control) {
$control->setOption('rendered', false);
}
}


/**
* Renders form begin.
*/
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
{
$el = $form->getElementPrototype();
$el->action = (string) $el->action;
$el = clone $el;
Expand Down
1 change: 1 addition & 0 deletions tests/Forms.Latte2/expected/FormMacros.button.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
%A%
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 1 */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
echo '>
Expand Down
4 changes: 3 additions & 1 deletion tests/Forms.Latte2/expected/FormMacros.formContainer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
%A%
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */;
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 1 */;
echo '
<table>
<tr>
Expand Down
15 changes: 12 additions & 3 deletions tests/Forms.Latte2/expected/FormMacros.forms.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
%A%
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], ['id' => 'myForm', 'class'=>"ajax"]) /* line 1 */;
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, ['id' => 'myForm', 'class'=>"ajax"]) /* line 1 */;
echo "\n";
$iterations = 0;
foreach (['id', 'username', 'select', 'area', 'send'] as $name) /* line 2 */ {
Expand Down Expand Up @@ -76,12 +78,16 @@
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 27 */;
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 27 */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
echo '
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 29 */;
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 29 */;
echo "\n";
$iterations = 0;
foreach ($form['sex']->items as $key => $label) /* line 31 */ {
Expand Down Expand Up @@ -197,6 +203,7 @@
';
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 58 */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
if (1) /* line 58 */ {
echo '<form id="myForm" class="ajax"';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['id' => null, 'class' => null], false);
Expand All @@ -214,6 +221,7 @@
';
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 63 */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<form';
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line 63 */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['class' => null], false);
Expand All @@ -230,6 +238,7 @@
';
$form = $this->global->formsStack[] = is_object($ʟ_tmp = $this->global->uiControl['myForm']) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp] /* line 68 */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<FORM';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
echo '>
Expand Down
5 changes: 4 additions & 1 deletion tests/Forms.Latte2/expected/FormMacros.get.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php
%A%
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */;
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line 1 */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
echo '
';
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 3 */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
echo '>
Expand Down
1 change: 1 addition & 0 deletions tests/Forms.Latte3/expected/forms.button.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo '>
Expand Down
1 change: 1 addition & 0 deletions tests/Forms.Latte3/expected/forms.formContainer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */;
echo '
<table>
Expand Down
2 changes: 2 additions & 0 deletions tests/Forms.Latte3/expected/forms.get.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack)) /* line %d% */;

echo '
';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo '>
Expand Down
7 changes: 7 additions & 0 deletions tests/Forms.Latte3/expected/forms.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, ['id' => 'myForm', 'class' => 'ajax']) /* line %d% */;
echo "\n";
foreach (['id', 'username', 'select', 'area', 'send'] as $name) /* line %d% */ {
Expand Down Expand Up @@ -64,13 +65,15 @@
';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */;
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack)) /* line %d% */;

echo '
';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */;
echo "\n";
foreach ($form['sex']->items as $key => $label) /* line %d% */ {
Expand Down Expand Up @@ -175,6 +178,7 @@
';
if (1) /* line %d% */ {
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['id' => null, 'class' => null], false) /* line %d% */;
echo ' id="myForm" class="ajax">
Expand All @@ -191,6 +195,7 @@
';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['class' => null], false) /* line %d% */;
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
Expand All @@ -208,6 +213,7 @@
';
$form = $this->global->formsStack[] = is_object($ʟ_tmp = $this->global->uiControl['myForm']) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<FORM';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo '>
Expand Down Expand Up @@ -250,6 +256,7 @@
';
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '
<label';
echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item('sex', $this->global))->getLabelPart()->attributes() /* line %d% */;
Expand Down
2 changes: 2 additions & 0 deletions tests/Forms.Latte3/n-name.form.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Assert::match(
<<<'XX'
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['foo'] /* line 1 */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line 1 */;
echo '>';
Expand All @@ -38,6 +39,7 @@ Assert::match(
<<<'XX'
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['foo'] /* line 1 */;
Nette\Bridges\FormsLatte\Runtime::initializeForm($form);
$ʟ_tag[0] = '';
if (0) /* line 1 */ {
echo '<';
Expand Down

0 comments on commit 9b3ccf1

Please sign in to comment.