Skip to content

Commit

Permalink
adds template component
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Nov 29, 2023
1 parent 36053d4 commit c6d6f5e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 13 deletions.
31 changes: 30 additions & 1 deletion src/base/ContextBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace markhuot\keystone\base;

class ContextBag
use Iterator;

class ContextBag implements Iterator
{
protected int $index = 0;

public function __construct(
protected array $context
) {
Expand All @@ -18,4 +22,29 @@ public function __get($key)
{
return $this->context[$key] ?? null;
}

public function current(): mixed
{
return $this->context[array_keys($this->context)[$this->index]];
}

public function key(): mixed
{
return array_keys($this->context)[$this->index];
}

public function next(): void
{
$this->index++;
}

public function rewind(): void
{
$this->index = 0;
}

public function valid(): bool
{
return isset(array_keys($this->context)[$this->index]);
}
}
15 changes: 14 additions & 1 deletion src/base/SlotDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use markhuot\keystone\models\Component;
use Twig\Markup;

use function markhuot\craftpest\helpers\test\dd;

class SlotDefinition
{
public function __construct(
Expand Down Expand Up @@ -98,6 +100,17 @@ public function render(array $context = []): Markup

public function __toString(): string
{
return $this->component->getSlot($this->name);
try {
return $this->component->getSlot($this->name);
} catch (\Throwable $e) {
// throw the previous exception, when present, because this
// exception is usually just that ->getSlot is not a string
if ($e->getPrevious()) {
throw $e->getPrevious();
}
else {
throw $e;
}
}
}
}
1 change: 1 addition & 0 deletions src/listeners/RegisterDefaultComponentTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function handle(RegisterComponentTypes $event): void
$event->registerTwigTemplate('keystone/section', 'cp:keystone/components/section.twig');
$event->registerTwigTemplate('keystone/tab', 'cp:keystone/components/tab.twig');
$event->registerTwigTemplate('keystone/tabs', 'cp:keystone/components/tabs.twig');
$event->registerTwigTemplate('keystone/template', 'cp:keystone/components/template.twig');
$event->registerTwigTemplate('keystone/text', 'cp:keystone/components/text.twig');
}
}
14 changes: 3 additions & 11 deletions src/templates/components/entry.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@
{# it would be nice to set `template` to a `craft\fields\Dropdown` but that causes the edit entry page to not render and redirect to the homepage without any errors #}
{% export propTypes = {
entry: field('craft\\fields\\Entries'),
template: field('craft\\fields\\Dropdown').options([
{label: 'Default', value: '' },
{label: 'Link', value: 'keystone/entry/link'},
]),
} %}
{% export summary = props.entry.one().title|default %}
{% export category = "Data" %}
{% set slot = component.getType().defineSlot() %}
{% for entry in props.entry %}
{% if props.template.value %}
{% include props.template.value with {entry: entry} only %}
{% else %}
{% do component.mergeContext({entry: entry}) %}
{{ slot }}
{% do component.mergeContext({entry: null}) %}
{% endif %}
{% do component.mergeContext({entry: entry}) %}
{{ slot }}
{% do component.mergeContext({entry: null}) %}
{% endfor %}
11 changes: 11 additions & 0 deletions src/templates/components/template.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% if props.template %}
{% set parts = props.template|split(':') %}
{% if parts|length == 2 %}
{% set templateMode = parts[0] %}
{% set template = parts[1] %}
{% else %}
{% set templateMode = 'site' %}
{% set template = parts[0] %}
{% endif %}
{{ craft.app.getView().renderTemplate(template, _context|merge(component.getContext()), templateMode)|raw }}
{% endif %}
29 changes: 29 additions & 0 deletions tests/components/TemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use markhuot\craftpest\factories\Entry;
use markhuot\craftpest\web\TestableResponse;
use markhuot\keystone\models\Component;

use function markhuot\craftpest\helpers\test\dd;

it('renders template component', function () {
$component = Component::factory()->type('keystone/template')->create();
$component->data->merge(['template' => 'basic-template'])->save();

expect(trim($component->render()))->toBe('foo');
});

it('renders entry links', function () {
$entry = Entry::factory()->create();

$entryComponent = Component::factory()->type('keystone/entry')->create();
$entryComponent->data->merge(['entry' => [$entry->id]])->save();

$templateComponent = Component::factory()->type('keystone/template')->path($entryComponent->id)->create();
$templateComponent->data->merge(['template' => 'cp:keystone/entry/link'])->save();

$html = $entryComponent->render();
expect((new TestableResponse(['content' => $html]))->querySelector('a'))
->getNodeOrNodes(fn ($node) => $node->getNode(0)->getAttribute('href'))->toBe($entry->uri)
->getText()->toBe($entry->title);
});
1 change: 1 addition & 0 deletions tests/templates/basic-template.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo

0 comments on commit c6d6f5e

Please sign in to comment.