99
1010namespace Nette \Bridges \FormsLatte ;
1111
12- use Nette ;
12+ use Nette \ Forms \ Container ;
1313use Nette \Forms \Form ;
1414use Nette \Utils \Html ;
1515
2020 */
2121class Runtime
2222{
23- use Nette \StaticClass;
24-
25- public static function initializeForm (Form $ form ): void
26- {
27- $ form ->fireRenderEvents ();
28- foreach ($ form ->getControls () as $ control ) {
29- $ control ->setOption ('rendered ' , false );
30- }
31- }
23+ /** @var Container[] */
24+ private array $ stack = [];
3225
3326
3427 /**
3528 * Renders form begin.
3629 */
37- public static function renderFormBegin (Form $ form , array $ attrs , bool $ withTags = true ): string
30+ public function renderFormBegin (array $ attrs , bool $ withTags = true ): string
3831 {
32+ $ form = $ this ->current ();
3933 $ el = $ form ->getElementPrototype ();
4034 $ el ->action = (string ) $ el ->action ;
4135 $ el = clone $ el ;
@@ -51,8 +45,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
5145 /**
5246 * Renders form end.
5347 */
54- public static function renderFormEnd (Form $ form , bool $ withTags = true ): string
48+ public function renderFormEnd (bool $ withTags = true ): string
5549 {
50+ $ form = $ this ->current ();
5651 $ s = '' ;
5752 if ($ form ->isMethod ('get ' )) {
5853 foreach (preg_split ('#[;&]# ' , (string ) parse_url ($ form ->getElementPrototype ()->action , PHP_URL_QUERY ), -1 , PREG_SPLIT_NO_EMPTY ) as $ param ) {
@@ -75,12 +70,35 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
7570 }
7671
7772
78- public static function item ($ item, $ global ): object
73+ public function item ($ item ): object
7974 {
80- if (is_object ($ item )) {
81- return $ item ;
75+ return is_object ($ item )
76+ ? $ item
77+ : $ this ->current ()[$ item ];
78+ }
79+
80+
81+ public function begin (Container $ form ): void
82+ {
83+ $ this ->stack [] = $ form ;
84+
85+ if ($ form instanceof Form) {
86+ $ form ->fireRenderEvents ();
87+ foreach ($ form ->getControls () as $ control ) {
88+ $ control ->setOption ('rendered ' , false );
89+ }
8290 }
83- $ form = end ($ global ->formsStack ) ?: throw new \LogicException ('Form declaration is missing, did you use {form} or <form n:name> tag? ' );
84- return $ form [$ item ];
91+ }
92+
93+
94+ public function end (): void
95+ {
96+ array_pop ($ this ->stack );
97+ }
98+
99+
100+ public function current (): Container
101+ {
102+ return end ($ this ->stack ) ?: throw new \LogicException ('Form declaration is missing, did you use {form} or <form n:name> tag? ' );
85103 }
86104}
0 commit comments