|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nextras community extensions of Nette Framework |
| 5 | + * |
| 6 | + * @license MIT |
| 7 | + * @link https://github.com/nextras/forms-rendering |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Nextras\FormsRendering\LatteMacros; |
| 11 | + |
| 12 | +use Latte\CompileException; |
| 13 | +use Latte\Compiler; |
| 14 | +use Latte\MacroNode; |
| 15 | +use Latte\Macros\MacroSet; |
| 16 | +use Latte\PhpWriter; |
| 17 | +use Nette\Forms\Controls\BaseControl; |
| 18 | +use Nette\Utils\Html; |
| 19 | +use Nextras; |
| 20 | + |
| 21 | + |
| 22 | +abstract class BaseInputMacros extends MacroSet |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @return self |
| 26 | + */ |
| 27 | + public static function install(Compiler $compiler): self |
| 28 | + { |
| 29 | + $me = new static($compiler); |
| 30 | + $me->addMacro('input', [$me, 'macroInput']); |
| 31 | + $me->addMacro('label', [$me, 'macroLabel'], [$me, 'macroLabelEnd']); |
| 32 | + return $me; |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + /** |
| 37 | + * {label ...} |
| 38 | + */ |
| 39 | + public function macroLabel(MacroNode $node, PhpWriter $writer): string |
| 40 | + { |
| 41 | + $class = get_class($this); |
| 42 | + $words = $node->tokenizer->fetchWords(); |
| 43 | + if (!$words) { |
| 44 | + throw new CompileException("Missing name in {{$node->name}}."); |
| 45 | + } |
| 46 | + $name = array_shift($words); |
| 47 | + return $writer->write( |
| 48 | + ($name[0] === '$' |
| 49 | + ? '$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word];' |
| 50 | + : '$_input = end($this->global->formsStack)[%0.word];' |
| 51 | + ) . 'if ($_label = $_input->%1.raw) echo ' . $class . '::label($_label->addAttributes(%node.array), $_input, %2.var)', |
| 52 | + $name, |
| 53 | + $words ? ('getLabelPart(' . implode(', ', array_map([$writer, 'formatWord'], $words)) . ')') : 'getLabel()', |
| 54 | + (bool) $words |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * {/label} |
| 61 | + */ |
| 62 | + public function macroLabelEnd(MacroNode $node, PhpWriter $writer): string |
| 63 | + { |
| 64 | + if ($node->content != null) { |
| 65 | + $node->openingCode = rtrim($node->openingCode, '?> ') . '->startTag() ?>'; |
| 66 | + return $writer->write('if ($_label) echo $_label->endTag()'); |
| 67 | + } else { |
| 68 | + return ''; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + /** |
| 74 | + * {input ...} |
| 75 | + */ |
| 76 | + public function macroInput(MacroNode $node, PhpWriter $writer): string |
| 77 | + { |
| 78 | + $class = get_class($this); |
| 79 | + $words = $node->tokenizer->fetchWords(); |
| 80 | + if (!$words) { |
| 81 | + throw new CompileException("Missing name in {{$node->name}}."); |
| 82 | + } |
| 83 | + $name = array_shift($words); |
| 84 | + return $writer->write( |
| 85 | + ($name[0] === '$' |
| 86 | + ? '$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word];' |
| 87 | + : '$_input = end($this->global->formsStack)[%0.word];' |
| 88 | + ) . 'echo ' . $class . '::input($_input->%1.raw->addAttributes(%node.array), $_input, %2.var)', |
| 89 | + $name, |
| 90 | + $words ? 'getControlPart(' . implode(', ', array_map([ |
| 91 | + $writer, |
| 92 | + 'formatWord', |
| 93 | + ], $words)) . ')' : 'getControl()', |
| 94 | + (bool) $words |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + public static function label(Html $label, BaseControl $control, bool $isSubItem): Html |
| 100 | + { |
| 101 | + return $label; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + public static function input(Html $input, BaseControl $control, bool $isSubItem): Html |
| 106 | + { |
| 107 | + return $input; |
| 108 | + } |
| 109 | +} |
0 commit comments