Skip to content

Commit 505820c

Browse files
author
Wazabii
committed
Nesting partial support
1 parent a095213 commit 505820c

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

SwiftRender.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,47 @@ public function withView(string $file, array $args = array()): self
206206
return $inst->view();
207207
}
208208

209+
/**
210+
* Create a partial view
211+
* @param string $file Filename
212+
* @param string $partial partial/key
213+
* @param array $args Args
214+
*/
215+
public function setPartial(string $partial, array $args = array()): self
216+
{
217+
218+
$file = $key2 = $key = $partial;
219+
220+
$partial = explode(".", $file);
221+
if(count($partial) > 1) {
222+
$key2 = $file = $partial[1];
223+
$key = $partial[0];
224+
225+
if(isset($partial[2])) {
226+
$key2 = "{$key}-{$partial[2]}";
227+
228+
}
229+
}
230+
231+
if (is_null($this->file)) {
232+
$this->setFile($file);
233+
}
234+
$func = $this->build($this->file, $args);
235+
236+
237+
238+
$this->partial[$key][$key2] = $func;
239+
240+
return $this;
241+
}
242+
209243
/**
210244
* Create a partial view
211245
* @param string $keyA Filename/key
212246
* @param string|array $keyB Args/filename
213247
* @param array $keyC Args
214248
*/
215-
public function setPartial(string $keyA, string|array $keyB = array(), array $keyC = array()): self
249+
public function setPartialOLD(string $keyA, string|array $keyB = array(), array $keyC = array()): self
216250
{
217251
$partial = $keyB;
218252
if (is_array($keyB)) {
@@ -224,7 +258,19 @@ public function setPartial(string $keyA, string|array $keyB = array(), array $ke
224258
$this->setFile($keyA);
225259
}
226260
$func = $this->build($this->file, $keyC);
227-
$this->partial[$partial][] = $func;
261+
262+
if(empty($this->partial[$partial]) || is_string($keyB)) {
263+
if(is_string($partial) && $partial[0] === "!") {
264+
$partial = substr($partial, 1);
265+
$this->partial[$partial] = [$func];
266+
267+
} else {
268+
$this->partial[$partial][] = $func;
269+
}
270+
271+
} else {
272+
$this->partial[$partial] = [$func];
273+
}
228274
return $this;
229275
}
230276

0 commit comments

Comments
 (0)