Skip to content

Commit

Permalink
added function hasBlock() [Closes #345]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 2, 2023
1 parent 0ae3566 commit 4765dff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Latte/Essential/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Latte\Compiler\Nodes\TextNode;
use Latte\Compiler\Tag;
use Latte\Compiler\TemplateParser;
use Latte\Runtime;
use Latte\RuntimeException;
use Nette;

Expand All @@ -28,6 +29,7 @@ final class CoreExtension extends Latte\Extension

private array $functions;
private bool $strict;
private Runtime\Template $template;


public function beforeCompile(Latte\Engine $engine): void
Expand All @@ -37,6 +39,12 @@ public function beforeCompile(Latte\Engine $engine): void
}


public function beforeRender(Runtime\Template $template): void
{
$this->template = $template;
}


public function getTags(): array
{
return [
Expand Down Expand Up @@ -180,6 +188,7 @@ public function getFunctions(): array
'last' => [Filters::class, 'last'],
'odd' => [Filters::class, 'odd'],
'slice' => [Filters::class, 'slice'],
'hasBlock' => fn(string $name): bool => $this->template->hasBlock($name),
];
}

Expand Down
23 changes: 23 additions & 0 deletions tests/tags/block.hasBlock.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

Assert::match(
<<<'EOD'
Main: true
Foo: false
EOD,
$latte->renderToString(<<<'EOD'
{block main}{/block}
Main: {=var_export(hasBlock(main), true)}
Foo: {=var_export(hasBlock(foo), true)}
EOD),
);

0 comments on commit 4765dff

Please sign in to comment.