1111
1212use Latte ;
1313use Nette ;
14+ use Nette \Forms \Container ;
1415use Nette \Forms \Form ;
1516use Nette \Utils \Html ;
1617
2122 */
2223class Runtime
2324{
24- use Nette \StaticClass;
25+ /** @var Container[] */
26+ private array $ stack = [];
27+
2528
2629 /**
2730 * Renders form begin.
2831 */
29- public static function renderFormBegin (Form $ form , array $ attrs , bool $ withTags = true ): string
32+ public function renderFormBegin (array $ attrs , bool $ withTags = true ): string
3033 {
34+ $ form = $ this ->current ();
3135 $ form ->fireRenderEvents ();
3236 foreach ($ form ->getControls () as $ control ) {
3337 $ control ->setOption ('rendered ' , false );
@@ -48,8 +52,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
4852 /**
4953 * Renders form end.
5054 */
51- public static function renderFormEnd (Form $ form , bool $ withTags = true ): string
55+ public function renderFormEnd (bool $ withTags = true ): string
5256 {
57+ $ form = $ this ->current ();
5358 $ s = '' ;
5459 if ($ form ->isMethod ('get ' )) {
5560 foreach (preg_split ('#[;&]# ' , (string ) parse_url ($ form ->getElementPrototype ()->action , PHP_URL_QUERY ), -1 , PREG_SPLIT_NO_EMPTY ) as $ param ) {
@@ -79,7 +84,7 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
7984 /**
8085 * Generates blueprint of form.
8186 */
82- public static function renderFormPrint (Form $ form ): void
87+ public function renderFormPrint (Form $ form ): void
8388 {
8489 $ blueprint = class_exists (Latte \Runtime \Blueprint::class)
8590 ? new Latte \Runtime \Blueprint
@@ -94,7 +99,7 @@ public static function renderFormPrint(Form $form): void
9499 /**
95100 * Generates blueprint of form data class.
96101 */
97- public static function renderFormClassPrint (Form $ form ): void
102+ public function renderFormClassPrint (Form $ form ): void
98103 {
99104 $ blueprint = class_exists (Latte \Runtime \Blueprint::class)
100105 ? new Latte \Runtime \Blueprint
@@ -110,12 +115,28 @@ public static function renderFormClassPrint(Form $form): void
110115 }
111116
112117
113- public static function item ($ item, $ global ): object
118+ public function item ($ item ): object
114119 {
115- if (is_object ($ item )) {
116- return $ item ;
117- }
118- $ form = end ($ global ->formsStack ) ?: throw new \LogicException ('Form declaration is missing, did you use {form} or <form n:name> tag? ' );
119- return $ form [$ item ];
120+ return is_object ($ item )
121+ ? $ item
122+ : $ this ->current ()[$ item ];
123+ }
124+
125+
126+ public function begin (Container $ form ): void
127+ {
128+ $ this ->stack [] = $ form ;
129+ }
130+
131+
132+ public function end (): void
133+ {
134+ array_pop ($ this ->stack );
135+ }
136+
137+
138+ public function current (): Container
139+ {
140+ return end ($ this ->stack ) ?: throw new \LogicException ('Form declaration is missing, did you use {form} or <form n:name> tag? ' );
120141 }
121142}
0 commit comments