@@ -135,15 +135,30 @@ public static function end(): void
135
135
}
136
136
137
137
/**
138
- * Closes any potentially open layout snippet.
138
+ * Renders the given template.
139
+ * This method supports the use of layout snippets inside the template.
139
140
*
140
- * @param Snippet|null $outsideSnippet The expected value for `Snippet::$current`.
141
- * @param string|null $template The content of the currently processed template or snippet.
142
- * @param array $data Additional data provided to any potential layout snippet.
143
- * @return string|null The new adjusted content of the template if `$template` was given.
141
+ * @param string|null $file Path to the template file.
142
+ * @param array $data Data available inside the template.
143
+ * @param array $layoutData Data available inside any potential layout snippet.
144
+ * @return string The rendered content of the given template file.
145
+ * @throws Throwable
146
+ *
147
+ * @see Tpl::load() Base implementation without support for layout snippets.
144
148
*/
145
- public static function endlayout (Snippet |null $ outsideSnippet , string |null $ template = null , array $ data = []): string |null
146
- {
149
+ public static function load (
150
+ string |null $ file = null ,
151
+ array $ data = [],
152
+ array $ layoutData = [],
153
+ ): string {
154
+ // if the template is rendered inside a snippet,
155
+ // we need to keep the "outside" snippet object
156
+ // to compare it later
157
+ $ outsideSnippet = Snippet::$ current ;
158
+
159
+ // load the template
160
+ $ template = parent ::load ($ file , $ data );
161
+
147
162
// if last `endsnippet()` inside the current template
148
163
// has been omitted (= snippet was used as layout snippet),
149
164
// `Snippet::$current` will point to a snippet that was
@@ -159,21 +174,16 @@ public static function endlayout(Snippet|null $outsideSnippet, string|null $temp
159
174
return $ template ;
160
175
}
161
176
162
- if (!isset ($ template )) {
163
- // let the snippet close and render natively
164
- echo static ::$ current ?->render($ data );
165
- return null ;
166
- }
167
177
if (Snippet::$ current ->slots ()->count () === 0 ) {
168
178
// no slots have been defined, but the template code
169
179
// should be used as default slot
170
- return Snippet::$ current ->render ($ data , [
180
+ return Snippet::$ current ->render ($ layoutData , [
171
181
'default ' => $ template
172
182
]);
173
183
}
174
184
// swallow any "unslotted" content
175
185
// between start and end
176
- return Snippet::$ current ->render ($ data );
186
+ return Snippet::$ current ->render ($ layoutData );
177
187
}
178
188
179
189
/**
@@ -289,16 +299,8 @@ public function render(array $data = [], array $slots = []): string
289
299
// custom data overrides for the data that was passed to the snippet instance
290
300
$ data = array_replace_recursive ($ this ->data , $ data );
291
301
292
- // if the template is rendered inside a snippet,
293
- // we need to keep the "outside" snippet object
294
- // to compare it later
295
- $ snippet = Snippet::$ current ;
296
-
297
302
// load the template representing this snippet
298
- $ template = static ::load ($ this ->file , static ::scope ($ data , $ this ->slots ()));
299
-
300
- // handle any potentially open layout snippet
301
- return static ::endlayout ($ snippet , $ template );
303
+ return static ::load ($ this ->file , static ::scope ($ data , $ this ->slots ()));
302
304
}
303
305
304
306
/**
0 commit comments