Skip to content

Commit

Permalink
allow rendering fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
withinboredom committed Aug 16, 2023
1 parent d1f0957 commit 19e0c0f
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 7 deletions.
81 changes: 80 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/Template/CompiledComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public function renderToString(): string

public function renderFragment(string $id): string {
$dom = $this->compile();
$fragment = $dom->createDocumentFragment();
foreach($dom->getElementById($id)->childNodes as $childNode) {
$fragment->appendChild($childNode->cloneNode(true));
}

return $this->compiler->renderCompiledHtml($dom->getElementById($id));
return $this->compiler->renderCompiledHtml($fragment);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/Template/Traits/Htmx.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,55 @@ private function historyPush(string|false $url): void
$this->headers->setHeader('HX-Push-Url', $url);
}

private function rerenderFragment(string $fragment, array $withState = [], string $prependHtml = ''): string {
$attributes = Attributes::forClass(static::class);

if(isset($this->serializer) && $_SERVER['HTTP_ACCEPT'] === 'application/json') {
return $this->serializer->serialize($withState, 'json');
}

if(isset($this->serializer) && $_SERVER['HTTP_ACCEPT'] === 'application/xml') {
return $this->serializer->serialize($withState, 'xml');
}

$attribute = null;

foreach($attributes->classAttributes as $attribute) {
if($attribute instanceof Component) {
$state = implode(
' ',
array_map(
static fn($key, $value) => "{$key}=\"{{$value}}\"",
array_keys($withState),
$withState
)
);
break;
}
}

if($attribute === null || !is_string($state)) {
throw new LogicException('Can not rerender a non-component in ' . static::class);
}

if(!isset($this->compiler)) {
throw new LogicException('Can not rerender without a compiler in ' . static::class);
}

if(empty($this->headers)) {
throw new LogicException('Can not rerender without Headers in ' . static::class);
}

$dom = "{$prependHtml}\n<{$attribute->name} {$state}></{$attribute->name}>";
$doc = $this->compiler->compile($dom);
$domFragment = $doc->createDocumentFragment();
foreach($doc->getElementById($fragment)->childNodes as $childNode) {
$domFragment->appendChild($childNode->cloneNode(true));
}

return $this->compiler->renderCompiledHtml($domFragment);
}

/**
* @param string $target_id
* @param array<mixed> $withState
Expand Down
2 changes: 2 additions & 0 deletions tests/SimpleApp/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public function render(string $name = '<>unknown<>'): string
return <<<HTML
<div>
Your name is {{$name}}.
<fragment id="fragment-test">
<todoItem id="stable" diamond ></todoItem>
</fragment>
<blah:label id="labeltest" for="stable">
This is a label
</blah:label>
Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/AppTest__it_renders_correctly__1.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
<body>
<testapp id="app"><div>
Your name is Rob&lt;&gt;is awesome.
<fragment id="fragment-test">
<todoitem id="stable"><div>
I am a diamond
</div></todoitem>
</fragment>
<label id="labeltest"> <label for="stable">

This is a label
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html><body>
<todoitem id="stable"><div>
I am a diamond
</div></todoitem>
</body></html>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<route></route>
<route></route>
<route>
<test id="cc9f0f89"> <div>hi</div>
<test id="c6512bd4"> <div>hi</div>
</test>
</route>
<defaultroute></defaultroute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
</head>
<body>
<route>
<test id="ca87ff67">
<test id="c8f14e45">
<div>
<h1>123</h1>
<test id="ce4da3b7">
<test id="cc9f0f89">
<div>
<h1>124</h1>
<test id="c1679091">
<test id="c45c48cc">
<div>
<h1>125</h1>
<test id="c8f14e45"> <div>hi</div>
<test id="cd3d9446"> <div>hi</div>
</test>
</div>

Expand Down

0 comments on commit 19e0c0f

Please sign in to comment.