Skip to content

Commit

Permalink
Handle [vars] section of comment ini (#401)
Browse files Browse the repository at this point in the history
* build: upgrade dom requirement and loosen version range

* docs: update examples

* feature: trim whitespace when there are only template children
closes #363

* feature: handle vars section in partial comment ini
closes #400

* tweak: nullable call

* tweak: stan improvement
  • Loading branch information
g105b authored Jan 30, 2023
1 parent dd5d399 commit 98beac1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
62 changes: 62 additions & 0 deletions examples/partial/01-basic-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\DocumentBinder;
use Gt\DomTemplate\PartialContent;
use Gt\DomTemplate\PartialExpander;

require(__DIR__ . "/../../vendor/autoload.php");
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Partials#pre-binding-using-template-variables

$partialContent = <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>My website :: {{site-heading ?? Home}}</title>
</head>
<body>
<h1 data-bind:text="site-heading">My website!</h1>
<main data-partial>
</main>
<footer>
<p>Thanks for visiting</p>
</footer>
</body>
</html>
HTML;

$pageAbout = <<<HTML
<!--
extends=main-template
[vars]
site-heading=About me
-->
<h2>About me</h2>
<p>This is my about me page on my amazing website!</p>
HTML;

$baseDirectory = sys_get_temp_dir() . "/phpgt-domtemplate-example";
$partialDirectory = "$baseDirectory/_partial";
mkdir($partialDirectory, 0775, true);
file_put_contents("$partialDirectory/main-template.html", $partialContent);

$document = new HTMLDocument($pageAbout);
$partial = new PartialContent($partialDirectory);
$expander = new PartialExpander($document, $partial);
$binder = new DocumentBinder($document);
$binder->cleanDatasets();
$expander->expand(binder: $binder);

echo $document;

// Remove the temporary files:
foreach(new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($baseDirectory, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
) as $file) {
if($file->isDir()) rmdir($file->getRealPath());
else unlink($file->getRealPath());
}
5 changes: 5 additions & 0 deletions src/CommentIni.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public function get(string $variable):?string {
return $var;
}

/** @return array<string, string> */
public function getVars():array {
return $this->iniData["vars"] ?? [];
}

public function containsIniData():bool {
return !empty($this->iniData);
}
Expand Down
17 changes: 16 additions & 1 deletion src/PartialExpander.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ class PartialExpander extends PartialContentExpander {
* @return string[] A list of names of partials that have been expanded,
* in the order that they were expanded.
*/
public function expand(Element $context = null):array {
public function expand(
?Element $context = null,
?DocumentBinder $binder = null,
):array {
if(!$context) {
$context = $this->document->documentElement;
}

$vars = [];
/** @var array<string, HTMLDocument> $partialDocumentArray */
$partialDocumentArray = [];
do {
Expand All @@ -23,6 +27,10 @@ public function expand(Element $context = null):array {
break;
}

if($commentVars = $commentIni->getVars()) {
$vars += $commentVars;
}

$partialDocument = $this->partialContent->getHTMLDocument($extends);
if(isset($partialDocumentArray[$extends])) {
throw new CyclicRecursionException("Partial '$extends' has already been expanded in this document, expanding again would cause cyclic recursion.");
Expand All @@ -46,6 +54,7 @@ public function expand(Element $context = null):array {
throw new PartialInjectionMultiplePointException("The current view extends the partial \"$extends\", but there is more than one element marked with `data-partial`. For help, see https://www.php.gt/domtemplate/partials");
}
$injectionPoint = $partialElementList[0] ?? null;
$partialElementList[0]?->removeAttribute("data-partial");

if(!$injectionPoint) {
throw new PartialInjectionPointNotFoundException("The current view extends the partial \"$extends\", but there is no element marked with `data-partial`. For help, see https://www.php.gt/domtemplate/partials");
Expand All @@ -69,6 +78,12 @@ public function expand(Element $context = null):array {
}
}

if($binder) {
foreach($vars as $key => $value) {
$binder->bindKeyValue($key, $value);
}
}

return array_keys($partialDocumentArray);
}
}
2 changes: 2 additions & 0 deletions test/phpunit/PartialContentTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace Gt\DomTemplate\Test;

use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\PartialContent;
use Gt\DomTemplate\PartialContentDirectoryNotFoundException;
use Gt\DomTemplate\PartialContentFileNotFoundException;
use Gt\DomTemplate\Test\TestFactory\DocumentTestFactory;
use PHPUnit\Framework\TestCase;

class PartialContentTest extends TestCase {
Expand Down
20 changes: 20 additions & 0 deletions test/phpunit/PartialExpanderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\CyclicRecursionException;
use Gt\DomTemplate\DocumentBinder;
use Gt\DomTemplate\PartialContent;
use Gt\DomTemplate\PartialContentFileNotFoundException;
use Gt\DomTemplate\PartialExpander;
Expand Down Expand Up @@ -58,6 +59,25 @@ public function testExpand():void {
self::assertSame("Hello from within a sub-template!", $mainElement->querySelector("h1")->textContent);
}

public function testExpand_commentVarsBound():void {
$partialContent = self::mockPartialContent(
"_partial", [
"base-page" => DocumentTestFactory::HTML_PARTIAL_VIEW
]
);
$document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$binder = self::createMock(DocumentBinder::class);
$binder->expects(self::once())
->method("bindKeyValue")
->with("title", "My website, extended...");

$sut = new PartialExpander(
$document,
$partialContent
);
$sut->expand(binder: $binder);
}

public function testExpand_noExtendsSectionOfCommentIni():void {
$document = new HTMLDocument();
$partialContent = self::createMock(PartialContent::class);
Expand Down

0 comments on commit 98beac1

Please sign in to comment.