Skip to content

Commit

Permalink
Support new template tag
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Aug 31, 2020
1 parent 8ab9894 commit 631e654
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 45 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^7.2",
"setono/tag-bag": "^1.0",
"setono/tag-bag": "^1.3",
"twig/twig": "^2.0 || ^3.0"
},
"require-dev": {
Expand Down
6 changes: 5 additions & 1 deletion src/Renderer/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Setono\TagBag\Renderer;

use Setono\TagBag\Tag\TagInterface;
use Setono\TagBag\Tag\TemplateTagInterface;
use Setono\TagBag\Tag\TwigTagInterface;
use Twig\Environment;

Expand All @@ -20,9 +21,12 @@ public function __construct(Environment $environment)

public function supports(TagInterface $tag): bool
{
return $tag instanceof TwigTagInterface;
return $tag instanceof TwigTagInterface || ($tag instanceof TemplateTagInterface && $tag->getTemplateType() === 'twig');
}

/**
* @param TemplateTagInterface|TagInterface $tag
*/
public function render(TagInterface $tag): string
{
return $this->environment->render($tag->getTemplate(), $tag->getContext());
Expand Down
32 changes: 2 additions & 30 deletions src/Tag/TwigTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,9 @@

namespace Setono\TagBag\Tag;

class TwigTag extends Tag implements TwigTagInterface
class TwigTag extends TemplateTag implements TwigTagInterface
{
/** @var string */
private $template;

/** @var array */
private $context;

public function __construct(string $template, array $context = [])
{
$this->setName('setono_tag_bag_twig_tag');
$this->template = $template;
$this->context = $context;
}

public function getTemplate(): string
{
return $this->template;
}

public function getContext(): array
{
return $this->context;
}

public function setContext(array $context): self
{
$this->context = $context;

return $this;
}
protected $name = 'setono_tag_bag_twig_tag';

/**
* @param mixed $value
Expand Down
11 changes: 1 addition & 10 deletions src/Tag/TwigTagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

namespace Setono\TagBag\Tag;

interface TwigTagInterface extends TagInterface
interface TwigTagInterface extends TemplateTagInterface
{
/**
* Returns the twig template
*/
public function getTemplate(): string;

/**
* Returns the context to inject into the twig template when rendered
*/
public function getContext(): array;
}
6 changes: 3 additions & 3 deletions tests/Renderer/TwigRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function it_supports_twig_tag_interface(): void
{
$renderer = new TwigRenderer($this->getEnvironment());

$this->assertTrue($renderer->supports(new TwigTag('template')));
self::assertTrue($renderer->supports(new TwigTag('template')));
}

/**
Expand All @@ -32,7 +32,7 @@ public function it_does_not_support_tag_interface(): void
{
$renderer = new TwigRenderer($this->getEnvironment());

$this->assertFalse($renderer->supports(new class() extends Tag {
self::assertFalse($renderer->supports(new class() extends Tag {
}));
}

Expand All @@ -54,7 +54,7 @@ public function it_renders(): void
'context_key' => 'context_value',
]);

$this->assertSame('content', $renderer->render($tag));
self::assertSame('content', $renderer->render($tag));
}

/**
Expand Down

0 comments on commit 631e654

Please sign in to comment.