Skip to content

Commit fd22bcb

Browse files
committed
Wrap Tpl::load by overriding it as Snippet::load
1 parent b0f4332 commit fd22bcb

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

src/Template/Snippet.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,30 @@ public static function end(): void
135135
}
136136

137137
/**
138-
* Closes any potentially open layout snippet.
138+
* Renders the given template.
139+
* This method supports the use of layout snippets inside the template.
139140
*
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.
144148
*/
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+
147162
// if last `endsnippet()` inside the current template
148163
// has been omitted (= snippet was used as layout snippet),
149164
// `Snippet::$current` will point to a snippet that was
@@ -159,21 +174,16 @@ public static function endlayout(Snippet|null $outsideSnippet, string|null $temp
159174
return $template;
160175
}
161176

162-
if (!isset($template)) {
163-
// let the snippet close and render natively
164-
echo static::$current?->render($data);
165-
return null;
166-
}
167177
if (Snippet::$current->slots()->count() === 0) {
168178
// no slots have been defined, but the template code
169179
// should be used as default slot
170-
return Snippet::$current->render($data, [
180+
return Snippet::$current->render($layoutData, [
171181
'default' => $template
172182
]);
173183
}
174184
// swallow any "unslotted" content
175185
// between start and end
176-
return Snippet::$current->render($data);
186+
return Snippet::$current->render($layoutData);
177187
}
178188

179189
/**
@@ -289,16 +299,8 @@ public function render(array $data = [], array $slots = []): string
289299
// custom data overrides for the data that was passed to the snippet instance
290300
$data = array_replace_recursive($this->data, $data);
291301

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-
297302
// 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()));
302304
}
303305

304306
/**

src/Template/Template.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Exception;
66
use Kirby\Cms\App;
77
use Kirby\Filesystem\F;
8-
use Kirby\Toolkit\Tpl;
98
use Stringable;
109

1110
/**
@@ -155,16 +154,7 @@ public function name(): string
155154
*/
156155
public function render(array $data = []): string
157156
{
158-
// if the template is rendered inside a snippet,
159-
// we need to keep the "outside" snippet object
160-
// to compare it later
161-
$snippet = Snippet::$current;
162-
163-
// load the template
164-
$template = Tpl::load($this->file(), $data);
165-
166-
// handle any potentially open layout snippet
167-
return Snippet::endlayout($snippet, $template, $data);
157+
return Snippet::load($this->file(), $data, $data);
168158
}
169159

170160
/**

0 commit comments

Comments
 (0)