99
1010namespace Nette \Bridges \FormsLatte ;
1111
12- use Nette ;
12+ use Nette \ Forms \ Container ;
1313use Nette \Forms \Form ;
1414use Nette \Utils \Html ;
1515use function end , explode , is_object , parse_url , preg_replace , preg_split , urldecode ;
2222 */
2323class Runtime
2424{
25- use Nette \StaticClass;
26-
27- public static function initializeForm (Form $ form ): void
28- {
29- $ form ->fireRenderEvents ();
30- foreach ($ form ->getControls () as $ control ) {
31- $ control ->setOption ('rendered ' , false );
32- }
33- }
25+ /** @var Container[] */
26+ private array $ stack = [];
3427
3528
3629 /**
3730 * Renders form begin.
3831 */
39- public static function renderFormBegin (Form $ form , array $ attrs , bool $ withTags = true ): string
32+ public function renderFormBegin (array $ attrs , bool $ withTags = true ): string
4033 {
34+ $ form = $ this ->current ();
4135 $ el = $ form ->getElementPrototype ();
4236 $ el ->action = (string ) $ el ->action ;
4337 $ el = clone $ el ;
@@ -53,8 +47,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
5347 /**
5448 * Renders form end.
5549 */
56- public static function renderFormEnd (Form $ form , bool $ withTags = true ): string
50+ public function renderFormEnd (bool $ withTags = true ): string
5751 {
52+ $ form = $ this ->current ();
5853 $ s = '' ;
5954 if ($ form ->isMethod ('get ' )) {
6055 foreach (preg_split ('#[;&]# ' , (string ) parse_url ($ form ->getElementPrototype ()->action , PHP_URL_QUERY ), -1 , PREG_SPLIT_NO_EMPTY ) as $ param ) {
@@ -77,12 +72,35 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
7772 }
7873
7974
80- public static function item ($ item, $ global ): object
75+ public function item ($ item ): object
8176 {
82- if (is_object ($ item )) {
83- return $ item ;
77+ return is_object ($ item )
78+ ? $ item
79+ : $ this ->current ()[$ item ];
80+ }
81+
82+
83+ public function begin (Container $ form ): void
84+ {
85+ $ this ->stack [] = $ form ;
86+
87+ if ($ form instanceof Form) {
88+ $ form ->fireRenderEvents ();
89+ foreach ($ form ->getControls () as $ control ) {
90+ $ control ->setOption ('rendered ' , false );
91+ }
8492 }
85- $ form = end ($ global ->formsStack ) ?: throw new \LogicException ('Form declaration is missing, did you use {form} or <form n:name> tag? ' );
86- return $ form [$ item ];
93+ }
94+
95+
96+ public function end (): void
97+ {
98+ array_pop ($ this ->stack );
99+ }
100+
101+
102+ public function current (): Container
103+ {
104+ return end ($ this ->stack ) ?: throw new \LogicException ('Form declaration is missing, did you use {form} or <form n:name> tag? ' );
87105 }
88106}
0 commit comments