Skip to content

Commit b2078aa

Browse files
committed
FormMacros: local $_form replaced with global $this->global->formsCurrent so it is not needed to pass $_form to included templates
$_form exists for BC # Conflicts: # tests/Forms.Latte/expected/FormMacros.forms.phtml
1 parent 47b80b4 commit b2078aa

File tree

5 files changed

+97
-92
lines changed

5 files changed

+97
-92
lines changed

src/Bridges/FormsLatte/FormMacros.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class FormMacros extends MacroSet
3131
public static function install(Latte\Compiler $compiler)
3232
{
3333
$me = new static($compiler);
34-
$me->addMacro('form', [$me, 'macroForm'], 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd($_form)');
35-
$me->addMacro('formContainer', [$me, 'macroFormContainer'], '$formContainer = $_form = array_pop($_formStack)');
34+
$me->addMacro('form', [$me, 'macroForm'], 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd($this->global->formsCurrent)');
35+
$me->addMacro('formContainer', [$me, 'macroFormContainer'], '$formContainer = $_form = $this->global->formsCurrent = array_pop($this->global->formsStack)');
3636
$me->addMacro('label', [$me, 'macroLabel'], [$me, 'macroLabelEnd'], NULL, self::AUTO_EMPTY);
3737
$me->addMacro('input', [$me, 'macroInput']);
3838
$me->addMacro('name', [$me, 'macroName'], [$me, 'macroNameEnd'], [$me, 'macroNameAttr']);
@@ -61,7 +61,7 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
6161
$node->replaced = true;
6262
$node->tokenizer->reset();
6363
return $writer->write(
64-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = '
64+
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsCurrent = '
6565
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
6666
. '$this->global->uiControl[%node.word], %node.array)'
6767
);
@@ -82,7 +82,9 @@ public function macroFormContainer(MacroNode $node, PhpWriter $writer)
8282
}
8383
$node->tokenizer->reset();
8484
return $writer->write(
85-
'$_formStack[] = $_form; $formContainer = $_form = ' . ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '') . '$_form[%node.word]'
85+
'$this->global->formsStack[] = $this->global->formsCurrent; $formContainer = $_form = $this->global->formsCurrent = '
86+
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
87+
. '$this->global->formsCurrent[%node.word]'
8688
);
8789
}
8890

@@ -102,7 +104,7 @@ public function macroLabel(MacroNode $node, PhpWriter $writer)
102104
$node->replaced = true;
103105
$name = array_shift($words);
104106
return $writer->write(
105-
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : $_form[%0.word]; if ($_label = $_input' : 'if ($_label = $_form[%0.word]')
107+
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : $this->global->formsCurrent[%0.word]; if ($_label = $_input' : 'if ($_label = $this->global->formsCurrent[%0.word]')
106108
. '->%1.raw) echo $_label'
107109
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : ''),
108110
$name,
@@ -138,7 +140,7 @@ public function macroInput(MacroNode $node, PhpWriter $writer)
138140
$node->replaced = true;
139141
$name = array_shift($words);
140142
return $writer->write(
141-
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : $_form[%0.word]; echo $_input' : 'echo $_form[%0.word]')
143+
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : $this->global->formsCurrent[%0.word]; echo $_input' : 'echo $this->global->formsCurrent[%0.word]')
142144
. '->%1.raw'
143145
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : ''),
144146
$name,
@@ -162,7 +164,7 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
162164

163165
if ($tagName === 'form') {
164166
return $writer->write(
165-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = '
167+
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsCurrent = '
166168
. ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '')
167169
. '$this->global->uiControl[%0.word], %1.var, FALSE)',
168170
$name,
@@ -172,7 +174,7 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
172174
$method = $tagName === 'label' ? 'getLabel' : 'getControl';
173175
return $writer->write(
174176
'$_input = ' . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '')
175-
. '$_form[%0.word]; echo $_input->%1.raw'
177+
. '$this->global->formsCurrent[%0.word]; echo $_input->%1.raw'
176178
. ($node->htmlNode->attrs ? '->addAttributes(%2.var)' : '') . '->attributes()',
177179
$name,
178180
$words
@@ -198,7 +200,7 @@ public function macroNameEnd(MacroNode $node, PhpWriter $writer)
198200
{
199201
$tagName = strtolower($node->htmlNode->name);
200202
if ($tagName === 'form') {
201-
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd($_form, FALSE) ?>';
203+
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd($this->global->formsCurrent, FALSE) ?>';
202204
} elseif ($tagName === 'label') {
203205
if ($node->htmlNode->isEmpty) {
204206
$node->innerContent = "<?php echo \$_input->{method_exists(\$_input, 'getLabelPart')?'getLabelPart':'getLabel'}()->getHtml() ?>";
@@ -226,9 +228,9 @@ public function macroInputError(MacroNode $node, PhpWriter $writer)
226228
if (!$name) {
227229
return $writer->write('echo %escape($_input->getError())');
228230
} elseif ($name[0] === '$') {
229-
return $writer->write('$_input = is_object(%0.word) ? %0.word : $_form[%0.word]; echo %escape($_input->getError())', $name);
231+
return $writer->write('$_input = is_object(%0.word) ? %0.word : $this->global->formsCurrent[%0.word]; echo %escape($_input->getError())', $name);
230232
} else {
231-
return $writer->write('echo %escape($_form[%0.word]->getError())', $name);
233+
return $writer->write('echo %escape($this->global->formsCurrent[%0.word]->getError())', $name);
232234
}
233235
}
234236

tests/Forms.Latte/expected/FormMacros.button.phtml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ class Template%a% extends Latte\Template
77
function render()
88
{
99
%A%<form<?php
10-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->uiControl["myForm"], array (
10+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsCurrent = $this->global->uiControl["myForm"], array (
1111
), FALSE) ?>>
1212
<button<?php
13-
$_input = $_form["send"];
13+
$_input = $this->global->formsCurrent["send"];
1414
echo $_input->{method_exists($_input, 'getControlPart')?'getControlPart':'getControl'}()->attributes() ?>>
1515
description of button
1616
</button>
1717

1818
<button<?php
19-
$_input = $_form["send"];
19+
$_input = $this->global->formsCurrent["send"];
2020
echo $_input->{method_exists($_input, 'getControlPart')?'getControlPart':'getControl'}()->attributes() ?>></button>
2121

2222
<button<?php
23-
$_input = $_form["send"];
23+
$_input = $this->global->formsCurrent["send"];
2424
echo $_input->{method_exists($_input, 'getControlPart')?'getControlPart':'getControl'}()->attributes() ?>><?php
2525
echo htmlspecialchars($_input->caption) ?></button>
26-
<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd($_form, FALSE) ?></form>
26+
<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd($this->global->formsCurrent, FALSE) ?></form>
2727
<?php
2828
}
2929

tests/Forms.Latte/expected/FormMacros.formContainer.phtml

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,53 @@ class Template%a% extends Latte\Template
77
function render()
88
{
99
%A%
10-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->uiControl["myForm"], []) ?>
10+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsCurrent = $this->global->uiControl["myForm"], []) ?>
1111

1212
<table>
1313
<tr>
14-
<th><?php if ($_label = $_form["input1"]->getLabel()) echo $_label ?></th>
15-
<td><?php echo $_form["input1"]->getControl() ?></td>
14+
<th><?php if ($_label = $this->global->formsCurrent["input1"]->getLabel()) echo $_label ?></th>
15+
<td><?php echo $this->global->formsCurrent["input1"]->getControl() ?></td>
1616
</tr>
1717
<?php
18-
$_formStack[] = $_form;
19-
$formContainer = $_form = $_form["cont1"] ?>
18+
$this->global->formsStack[] = $this->global->formsCurrent;
19+
$formContainer = $_form = $this->global->formsCurrent = $this->global->formsCurrent["cont1"] ?>
2020
<tr>
21-
<th><?php if ($_label = $_form["input2"]->getLabel()) echo $_label ?></th>
22-
<td><?php echo $_form["input2"]->getControl() ?></td>
21+
<th><?php if ($_label = $this->global->formsCurrent["input2"]->getLabel()) echo $_label ?></th>
22+
<td><?php echo $this->global->formsCurrent["input2"]->getControl() ?></td>
2323
</tr>
2424
<tr>
25-
<th><?php if ($_label = $_form["input3"]->getLabel()) echo $_label ?></th>
26-
<td><?php echo $_form["input3"]->getControl() ?></td>
25+
<th><?php if ($_label = $this->global->formsCurrent["input3"]->getLabel()) echo $_label ?></th>
26+
<td><?php echo $this->global->formsCurrent["input3"]->getControl() ?></td>
2727
</tr>
2828
<tr>
2929
<th>Checkboxes</th>
3030
<td>
3131
<?php
32-
$_formStack[] = $_form;
33-
$formContainer = $_form = $_form["cont2"] ?> <ol>
32+
$this->global->formsStack[] = $this->global->formsCurrent;
33+
$formContainer = $_form = $this->global->formsCurrent = $this->global->formsCurrent["cont2"] ?> <ol>
3434
<?php
3535
$iterations = 0;
3636
if (isset($this->params['name'])) trigger_error('Variable $name overwritten in foreach.');
3737
if (isset($this->params['field'])) trigger_error('Variable $field overwritten in foreach.');
3838
foreach ($formContainer->controls AS $name => $field) {
3939
?> <li><?php
40-
$_input = is_object($field) ? $field : $_form[$field];
40+
$_input = is_object($field) ? $field : $this->global->formsCurrent[$field];
4141
echo $_input->getControl() ?></li>
4242
<?php
4343
$iterations++;
4444
}
4545
?> </ol>
46-
<?php $formContainer = $_form = array_pop($_formStack) ?>
46+
<?php $formContainer = $_form = $this->global->formsCurrent = array_pop($this->global->formsStack) ?>
4747
</td>
4848
</tr>
4949
<tr>
50-
<th><?php if ($_label = $_form["input7"]->getLabel()) echo $_label ?></th>
51-
<td><?php echo $_form["input7"]->getControl() ?></td>
50+
<th><?php if ($_label = $this->global->formsCurrent["input7"]->getLabel()) echo $_label ?></th>
51+
<td><?php echo $this->global->formsCurrent["input7"]->getControl() ?></td>
5252
</tr>
5353
<?php
54-
$formContainer = $_form = array_pop($_formStack);
55-
$_formStack[] = $_form;
56-
$formContainer = $_form = $_form["items"] ?>
54+
$formContainer = $_form = $this->global->formsCurrent = array_pop($this->global->formsStack);
55+
$this->global->formsStack[] = $this->global->formsCurrent;
56+
$formContainer = $_form = $this->global->formsCurrent = $this->global->formsCurrent["items"] ?>
5757
<tr>
5858
<th>Items</th>
5959
<td>
@@ -63,23 +63,24 @@ class Template%a% extends Latte\Template
6363
if (isset($this->params['item'])) trigger_error('Variable $item overwritten in foreach.');
6464
foreach ($items as $item) {
6565
if (!isset($formContainer[$item])) continue;
66-
$_formStack[] = $_form;
67-
$formContainer = $_form = is_object($item) ? $item : $_form[$item] ?> <?php echo $_form["input"]->getControl() ?>
66+
$this->global->formsStack[] = $this->global->formsCurrent;
67+
$formContainer = $_form = $this->global->formsCurrent = is_object($item) ? $item : $this->global->formsCurrent[$item] ?> <?php
68+
echo $this->global->formsCurrent["input"]->getControl() ?>
6869

6970
<?php
70-
$formContainer = $_form = array_pop($_formStack);
71+
$formContainer = $_form = $this->global->formsCurrent = array_pop($this->global->formsStack);
7172
$iterations++;
7273
}
7374
?>
7475
</td>
7576
</tr>
76-
<?php $formContainer = $_form = array_pop($_formStack) ?>
77+
<?php $formContainer = $_form = $this->global->formsCurrent = array_pop($this->global->formsStack) ?>
7778
<tr>
78-
<th><?php if ($_label = $_form["input8"]->getLabel()) echo $_label ?></th>
79-
<td><?php echo $_form["input8"]->getControl() ?></td>
79+
<th><?php if ($_label = $this->global->formsCurrent["input8"]->getLabel()) echo $_label ?></th>
80+
<td><?php echo $this->global->formsCurrent["input8"]->getControl() ?></td>
8081
</tr>
8182
</table>
82-
<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd($_form) ?>
83+
<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd($this->global->formsCurrent) ?>
8384

8485
<?php
8586
}

0 commit comments

Comments
 (0)