Skip to content

Commit 7086964

Browse files
committed
FormMacros: added {formContext}
1 parent b4d63f4 commit 7086964

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/Bridges/FormsLatte/FormMacros.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
* - {label name /} or {label name}... {/label}
2525
* - {inputError name}
2626
* - {formContainer name} ... {/formContainer}
27+
* - {formContext name} ... {/formContext}
2728
*/
2829
final class FormMacros extends MacroSet
2930
{
3031
public static function install(Latte\Compiler $compiler): void
3132
{
3233
$me = new static($compiler);
3334
$me->addMacro('form', [$me, 'macroForm'], 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));');
35+
$me->addMacro('formContext', [$me, 'macroFormContext'], 'array_pop($this->global->formsStack);');
3436
$me->addMacro('formContainer', [$me, 'macroFormContainer'], 'array_pop($this->global->formsStack); $formContainer = $_form = end($this->global->formsStack)');
3537
$me->addMacro('label', [$me, 'macroLabel'], [$me, 'macroLabelEnd'], null, self::AUTO_EMPTY);
3638
$me->addMacro('input', [$me, 'macroInput']);
@@ -69,6 +71,30 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
6971
}
7072

7173

74+
/**
75+
* {formContext ...}
76+
*/
77+
public function macroFormContext(MacroNode $node, PhpWriter $writer)
78+
{
79+
if ($node->modifiers) {
80+
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
81+
}
82+
if ($node->prefix) {
83+
throw new CompileException('Did you mean <form n:name=...> ?');
84+
}
85+
$name = $node->tokenizer->fetchWord();
86+
if ($name == null) { // null or false
87+
throw new CompileException('Missing form name in ' . $node->getNotation());
88+
}
89+
$node->tokenizer->reset();
90+
return $writer->write(
91+
'$form = $this->global->formsStack[] = '
92+
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
93+
. '$this->global->uiControl[%node.word];'
94+
);
95+
}
96+
97+
7298
/**
7399
* {formContainer ...}
74100
*/

tests/Forms.Latte/expected/FormMacros.forms.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,8 @@
117117
<select name="select" id="frm-select"><option value="m">male</option><option value="f">female</option></select>
118118
<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
119119
</form>
120+
121+
122+
123+
<label>Sex:</label>
124+
<input type="text" name="username" class="control-class" id="frm-username">

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,17 @@
220220
<?php
221221
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
222222
?>
223+
224+
225+
226+
<?php
227+
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"];
228+
?><label<?php
229+
$_input = end($this->global->formsStack)["sex"];
230+
echo $_input->getLabelPart()->attributes() ?>><?php echo $_input->getLabelPart()->getHtml() ?></label>
231+
<input<?php
232+
$_input = end($this->global->formsStack)["username"];
233+
echo $_input->getControlPart()->attributes() ?>>
234+
<?php
235+
array_pop($this->global->formsStack);
223236
%A%

tests/Forms.Latte/templates/forms.latte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,9 @@
7878

7979
<select n:name="select" />
8080
{/form}
81+
82+
83+
{formContext myForm}
84+
<label n:name="sex" />
85+
<input n:name=username>
86+
{/formContext myForm}

0 commit comments

Comments
 (0)