Skip to content

Commit

Permalink
FormMacros: added {formPrint $form}
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 23, 2020
1 parent fd5d69d commit bdfb7de
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Bridges/FormsLatte/FormMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Latte\MacroNode;
use Latte\Macros\MacroSet;
use Latte\PhpWriter;
use Nette\Forms;


/**
Expand All @@ -36,6 +37,7 @@ public static function install(Latte\Compiler $compiler): void
$me->addMacro('input', [$me, 'macroInput']);
$me->addMacro('name', [$me, 'macroName'], [$me, 'macroNameEnd'], [$me, 'macroNameAttr']);
$me->addMacro('inputError', [$me, 'macroInputError']);
$me->addMacro('formPrint', [$me, 'macroFormPrint']);
}


Expand Down Expand Up @@ -240,4 +242,57 @@ public function macroInputError(MacroNode $node, PhpWriter $writer)
return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError());', $name);
}
}


/**
* {formPrint [ClassName]}
*/
public function macroFormPrint(MacroNode $node, PhpWriter $writer)
{
$name = $node->tokenizer->fetchWord();
if ($name == null) { // null or false
throw new CompileException('Missing form name in ' . $node->getNotation());
}
$node->tokenizer->reset();
return $writer->write(
__CLASS__ . '::macroFromPrintRuntime('
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
. '$this->global->uiControl[%node.word]);'
);
}


/**
* Generates blueprint of form.
*/
public static function macroFromPrintRuntime($form): void
{
$dummy = new Forms\Form;
foreach ($form->getControls() as $name => $input) {
if ($input instanceof Forms\Controls\Hidden) {
continue;
}
$dummyControl = new class extends Forms\Controls\BaseControl {
public function getLabel($name = null)
{
return '{label ' . $this->getName() . '}';
}


public function getControl()
{
return '{input ' . $this->getName() . '}';
}
};
$dummy->addComponent($dummyControl->setRequired($input->isRequired()), (string) $name);
}
$dummy->setRenderer($form->getRenderer());

ob_end_clean();
header('Content-Type: text/plain');
echo '<form n:name="' . $form->getName() . '">';
echo $dummy->render('body');
echo '</form>';
exit;
}
}

0 comments on commit bdfb7de

Please sign in to comment.