|
9 | 9 |
|
10 | 10 | namespace Nette\Bridges\FormsLatte; |
11 | 11 |
|
| 12 | +use Latte; |
12 | 13 | use Nette; |
13 | 14 | use Nette\Forms\Form; |
14 | 15 | use Nette\Utils\Html; |
@@ -71,4 +72,68 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string |
71 | 72 |
|
72 | 73 | return $s . ($withTags ? $form->getElementPrototype()->endTag() . "\n" : ''); |
73 | 74 | } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Generates blueprint of form. |
| 79 | + */ |
| 80 | + public static function renderBlueprint($form): void |
| 81 | + { |
| 82 | + $dummyForm = new Form; |
| 83 | + $dict = new \SplObjectStorage; |
| 84 | + foreach ($form->getControls() as $name => $input) { |
| 85 | + $dict[$input] = $dummyControl = new class extends Nette\Forms\Controls\BaseControl { |
| 86 | + public $inner; |
| 87 | + |
| 88 | + |
| 89 | + public function getLabel($name = null) |
| 90 | + { |
| 91 | + return $this->inner->getLabel() ? '{label ' . $this->inner->lookupPath(null) . '}' : null; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + public function getControl() |
| 96 | + { |
| 97 | + return '{input ' . $this->inner->lookupPath(null) . '}'; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + public function isRequired(): bool |
| 102 | + { |
| 103 | + return $this->inner->isRequired(); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + public function getOption($key, $default = null) |
| 108 | + { |
| 109 | + return $key === 'rendered' ? parent::getOption($key) : $this->inner->getOption($key, $default); |
| 110 | + } |
| 111 | + }; |
| 112 | + $dummyControl->inner = $input; |
| 113 | + $dummyForm->addComponent($dummyControl, (string) $dict->count()); |
| 114 | + } |
| 115 | + |
| 116 | + foreach ($form->getGroups() as $group) { |
| 117 | + $dummyGroup = $dummyForm->addGroup(); |
| 118 | + foreach ($group->getOptions() as $k => $v) { |
| 119 | + $dummyGroup->setOption($k, $v); |
| 120 | + } |
| 121 | + foreach ($group->getControls() as $control) { |
| 122 | + if ($dict[$control]) { |
| 123 | + $dummyGroup->add($dict[$control]); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + $dummyForm->setRenderer($form->getRenderer()); |
| 129 | + ob_start(); |
| 130 | + $dummyForm->render('body'); |
| 131 | + $body = ob_get_clean(); |
| 132 | + |
| 133 | + $blueprint = new Latte\Runtime\Blueprint; |
| 134 | + $end = $blueprint->printCanvas(); |
| 135 | + $blueprint->printHeader('Form ' . $form->getName()); |
| 136 | + $blueprint->printCode('<form n:name="' . $form->getName() . '">' . $body . '</form>'); |
| 137 | + echo $end; |
| 138 | + } |
74 | 139 | } |
0 commit comments