From e21d7be7254e1ef1de8b10cffecb082702114189 Mon Sep 17 00:00:00 2001 From: Fernando Gutierrez Date: Sun, 4 Aug 2024 23:17:30 -0600 Subject: [PATCH] 5.4.0 - Update Snippet class for slot support - Refactor Template class --- composer.json | 2 +- index.php | 6 +- src/Snippet.php | 68 +++++++++++----------- src/Template.php | 11 ++-- src/View/Compiler/ComponentTagCompiler.php | 1 - 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/composer.json b/composer.json index 4e17049..62b587f 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "keywords": [ "kirby", "kirby-3", "kirby-4", "blade", "view", "template" ], - "version": "5.3.2", + "version": "5.4.0", "type": "kirby-plugin", "license": "MIT", "authors": [ diff --git a/index.php b/index.php index 992a3a5..e80b6a6 100644 --- a/index.php +++ b/index.php @@ -5,13 +5,13 @@ use Illuminate\Support\Str; use Kirby\Cms\App as Kirby; use Kirby\Http\Header; +use Kirby\Template\Snippet as KirbySnippet; @include_once __DIR__.'/vendor/autoload.php'; Kirby::plugin('beebmx/kirby-blade', [ 'options' => require_once __DIR__.'/extensions/options.php', 'hooks' => require_once __DIR__.'/extensions/hooks.php', - 'components' => [ 'template' => function (Kirby $kirby, string $name, ?string $contentType = null) { if (Str::endsWith($kirby->request()->url(), '.php')) { @@ -20,8 +20,8 @@ return new Template($kirby, $name, $contentType); }, - 'snippet' => function (Kirby $kirby, string $name, array $data = []) { - return (new Snippet($kirby, $name))->render($data); + 'snippet' => function (Kirby $kirby, string $name, array $data = [], bool $slots = false): KirbySnippet|string { + return Snippet::factory($name, $data, $slots); }, ], ]); diff --git a/src/Snippet.php b/src/Snippet.php index 486945e..0b02d58 100644 --- a/src/Snippet.php +++ b/src/Snippet.php @@ -4,52 +4,54 @@ use Beebmx\Blade\Container; use Beebmx\KirbyBlade\Blade; +use Kirby\Cms\App; use Kirby\Cms\App as Kirby; -use Kirby\Toolkit\Tpl; +use Kirby\Template\Snippet as KirbySnippet; -class Snippet extends Template +class Snippet extends KirbySnippet { - protected string $snippet; + /** + * Returns either an open snippet capturing slots + * or the template string for self-enclosed snippets + */ + public static function factory( + string|array|null $name, + array $data = [], + bool $slots = false + ): static|string { + if (static::isBlade($name)) { + $blade = new Blade( + static::getPathSnippets(), + Template::getPathViews(), + new Container + ); - public function __construct(Kirby $kirby, string $name, string $type = 'html', string $defaultType = 'html') - { - $this->template = static::getPathSnippets(); - $this->views = static::getPathViews(); - $this->snippet = $this->template.'/'.$name.'.php'; + return $blade->make($name, $data); + } - $blade = $this->template.'/'.$name.'.'.$this->bladeExtension(); + // instead of returning empty string when `$name` is null + // allow rest of code to run, otherwise the wrong snippet would be closed + // and potential issues for nested snippets may occur + $file = $name !== null ? static::file($name) : null; - if (file_exists($this->snippet) === false && file_exists($blade) === false) { - $this->snippet = $kirby->extensions('snippets')[$name]; + // for snippets with slots, make sure to open a new + // snippet and start capturing slots + if ($slots === true) { + return static::begin($file, $data); } - $this->name = strtolower($name); - $this->type = $type; - $this->defaultType = $defaultType; - - $this->setViewDirectory(); + // for snippets without slots, directly load and return + // the snippet's template file + return static::load($file, static::scope($data)); } - public function render(array $data = []): string + public static function getPathSnippets(): string { - if ($this->isBlade()) { - $this->blade = new Blade( - $this->template, - $this->views, - new Container - ); - - $this->setDirectives(); - $this->setIfStatements(); - - return $this->blade->make($this->name, $data); - } else { - return Tpl::load($this->snippet, $data); - } + return Kirby::instance()->roots()->snippets(); } - public static function getPathSnippets(): string + public static function isBlade(string $name): bool { - return Kirby::instance()->roots()->snippets(); + return file_exists(App::instance()->roots()->snippets().'/'.$name.'.'.Template::BLADE_EXTENSION); } } diff --git a/src/Template.php b/src/Template.php index 7ac743b..a516865 100644 --- a/src/Template.php +++ b/src/Template.php @@ -15,6 +15,8 @@ class Template extends KirbyTemplate { + public const BLADE_EXTENSION = 'blade.php'; + protected Blade $blade; protected string $views; @@ -76,7 +78,7 @@ public function file(): ?string public function render(array $data = []): string { if ($this->isBlade()) { - $application = $this->getContainer( + $application = static::getContainer( Container::getInstance() ); @@ -271,7 +273,7 @@ public function getFilename(?string $name = null): string public function isBlade(): bool { - return (bool) file_exists($this->template.'/'.$this->name().'.'.$this->bladeExtension()); + return file_exists($this->template.'/'.$this->name().'.'.$this->bladeExtension()); } /** @@ -279,7 +281,7 @@ public function isBlade(): bool */ public function bladeExtension(): string { - return 'blade.php'; + return Template::BLADE_EXTENSION; } public static function getPathTemplates(): string @@ -290,6 +292,7 @@ public static function getPathTemplates(): string public static function getPathViews(): string { $path = Kirby::instance()->option('beebmx.kirby-blade.views'); + if (is_callable($path)) { return $path(); } @@ -297,7 +300,7 @@ public static function getPathViews(): string return $path; } - protected function getContainer(ContainerInterface $container): ContainerInterface + public static function getContainer(ContainerInterface $container): ContainerInterface { return method_exists($container, 'terminate') ? $container diff --git a/src/View/Compiler/ComponentTagCompiler.php b/src/View/Compiler/ComponentTagCompiler.php index 87dcaea..20e5e42 100644 --- a/src/View/Compiler/ComponentTagCompiler.php +++ b/src/View/Compiler/ComponentTagCompiler.php @@ -2,7 +2,6 @@ namespace Beebmx\View\Compiler; - use Beebmx\View\DynamicComponent; use Illuminate\Container\Container; use Illuminate\Support\Str;