Skip to content

Commit

Permalink
5.4.0
Browse files Browse the repository at this point in the history
- Update Snippet class for slot support
- Refactor Template class
  • Loading branch information
beebmx committed Aug 5, 2024
1 parent 06b1db6 commit e21d7be
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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);
},
],
]);
68 changes: 35 additions & 33 deletions src/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
11 changes: 7 additions & 4 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class Template extends KirbyTemplate
{
public const BLADE_EXTENSION = 'blade.php';

protected Blade $blade;

protected string $views;
Expand Down Expand Up @@ -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()
);

Expand Down Expand Up @@ -271,15 +273,15 @@ 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());
}

/**
* Returns the expected template file extension
*/
public function bladeExtension(): string
{
return 'blade.php';
return Template::BLADE_EXTENSION;
}

public static function getPathTemplates(): string
Expand All @@ -290,14 +292,15 @@ public static function getPathTemplates(): string
public static function getPathViews(): string
{
$path = Kirby::instance()->option('beebmx.kirby-blade.views');

if (is_callable($path)) {
return $path();
}

return $path;
}

protected function getContainer(ContainerInterface $container): ContainerInterface
public static function getContainer(ContainerInterface $container): ContainerInterface
{
return method_exists($container, 'terminate')
? $container
Expand Down
1 change: 0 additions & 1 deletion src/View/Compiler/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Beebmx\View\Compiler;


use Beebmx\View\DynamicComponent;
use Illuminate\Container\Container;
use Illuminate\Support\Str;
Expand Down

0 comments on commit e21d7be

Please sign in to comment.