From 35a7bd4cf7d12eb5315ce0b7cfd3fb822cfb56fd Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 21 Oct 2024 18:09:33 +0000 Subject: [PATCH] - Block can now reference a 'this' object from it's {% using [ 'this' => new stdClass() ] %} token - Fixed a bug with {% use %} which was buggy whenever two uses were declared and one of them had a curly braces notation in it. --- src/Builder.php | 2 +- src/Compiler/Context.php | 2 +- src/ControlStructure/BlockToken.php | 7 ++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index a541802..990880a 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -21,7 +21,7 @@ class Builder $replace = [ '%NAMESPACE%' => $context->namespace, - '%USE%' => ( $uses = $context->renderUses() ) ? "use $uses;" : false, + '%USE%' => ( $uses = $context->renderUses() ) ? $uses : false, '%CLASSNAME%' => $context->className, '%PATHNAME%' => $path($context->viewPath), '%FULLPATH%' => $path($context->filePath), diff --git a/src/Compiler/Context.php b/src/Compiler/Context.php index 51c6170..09db1e7 100644 --- a/src/Compiler/Context.php +++ b/src/Compiler/Context.php @@ -57,7 +57,7 @@ abstract class Context { public function renderUses() : string { - return implode(",", $this->useStack ?? []); + return implode(PHP_EOL, array_map(fn($use) => "use $use;", $this->useStack ?? [])); } public function renderFunctions() : string diff --git a/src/ControlStructure/BlockToken.php b/src/ControlStructure/BlockToken.php index 8b728b5..ca05514 100644 --- a/src/ControlStructure/BlockToken.php +++ b/src/ControlStructure/BlockToken.php @@ -227,7 +227,12 @@ class BlockToken implements ControlStructure { { $this->rendering = true; - return $classTemplate->picea->inlineBlock($this, $this->viewPath, ...$this->arguments); + if ($this->using['this'] ?? false) { + $thisProxy = $this->using['this']; + unset($this->using['this']); + } + + return $classTemplate->picea->inlineBlock($thisProxy ?? $this, $this->viewPath, ...$this->arguments); } public function setSlot(string $name, Callable $method) : void