Skip to content

Commit

Permalink
Merge pull request #7 from steefmin/full-template-render
Browse files Browse the repository at this point in the history
Full template render
  • Loading branch information
tomcri authored Oct 7, 2024
2 parents 21f496e + c2e5f18 commit 83ef23d
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/Controller/HtmxControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Htmxfony\Response\HtmxRefreshResponse;
use Htmxfony\Response\HtmxResponse;
use Htmxfony\Response\HtmxStopPollingResponse;
use Htmxfony\Template\Template;
use Htmxfony\Template\TemplateBlock;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
Expand Down Expand Up @@ -59,4 +60,14 @@ protected function htmxStopPolling(): HtmxStopPollingResponse
return new HtmxStopPollingResponse();
}

protected function htmxRenderTemplate(Template ...$templates): HtmxResponse
{
$content = '';
foreach ($templates as $template) {
$content .= $this->container->get('twig')->render($template->getTemplateFileName(), $template->getContext());
}

return new HtmxResponse($content);
}

}
32 changes: 32 additions & 0 deletions src/Template/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Htmxfony\Template;

class Template
{

private $view;

private $context;

public function __construct(
string $view,
array $context = []
) {
$this->view = $view;
$this->context = $context;
}

public function getTemplateFileName(): string
{
return $this->view;
}

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

}
11 changes: 4 additions & 7 deletions src/Template/TemplateBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@
class TemplateBlock
{

private $templateFileName;

private $blockName;

private $context;
private $template;

public function __construct(
string $templateFileName,
string $blockName,
array $context = []
) {
$this->context = $context;
$this->blockName = $blockName;
$this->templateFileName = $templateFileName;
$this->template = new Template($templateFileName, $context);
}

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

public function getBlockName(): string
Expand All @@ -35,7 +32,7 @@ public function getBlockName(): string

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

}
37 changes: 37 additions & 0 deletions tests/Controller/HtmxControllerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Htmxfony\Response\HtmxRefreshResponse;
use Htmxfony\Response\HtmxResponse;
use Htmxfony\Response\HtmxStopPollingResponse;
use Htmxfony\Template\Template;
use Htmxfony\Template\TemplateBlock;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -185,4 +186,40 @@ public function index(): HtmxStopPollingResponse
$this->assertSame(HtmxStopPollingResponse::HX_CODE_STOP_POLLING, $response->getStatusCode());
}

public function testRenderTemplate(): void
{
$controller = new class ($this->container) extends AbstractController
{
use HtmxControllerTrait;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function index(HtmxRequest $request): HtmxResponse
{
return $this->htmxRenderTemplate(
new Template(
'test.html.twig',
['value1' => $request->get('value1'), 'value2' => $request->get('value2')]
),
new Template(
'test.html.twig',
['value1' => 'val3', 'value2' => 'val4']
)
);
}

};

$htmxRequest = new HtmxRequest();
$htmxRequest->request->set('value1', 'test1');
$htmxRequest->request->set('value2', 'test2');
$response = $controller->index($htmxRequest);

$this->assertEquals('[Text outside blocks]test1test2[Text outside blocks]val3val4', $response->getContent());
$this->assertInstanceOf(HtmxResponse::class, $response);
}

}
23 changes: 23 additions & 0 deletions tests/Template/TemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Htmxfony\Tests\Template;

use Htmxfony\Template\Template;
use PHPUnit\Framework\TestCase;

class TemplateTest extends TestCase
{

public function testTemplateBlock(): void
{
$templateBlock = new Template(
'templateFileName',
['prop' => 'val'],
);
$this->assertSame('templateFileName', $templateBlock->getTemplateFileName());
$this->assertSame(['prop' => 'val'], $templateBlock->getContext());
}

}

0 comments on commit 83ef23d

Please sign in to comment.