Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle [vars] section of comment ini #401

Merged
merged 19 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
00d1189
build: upgrade dom requirement and loosen version range
g105b Jul 24, 2022
3fcbcb9
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Aug 14, 2022
2a79afb
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Aug 18, 2022
5d58fc4
docs: update examples
g105b Aug 18, 2022
e3957a8
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Aug 26, 2022
73d0b85
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Sep 21, 2022
3cbb825
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Oct 5, 2022
fadbba7
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Oct 8, 2022
5403158
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Oct 31, 2022
224999b
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 10, 2023
3def753
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 17, 2023
3dbda21
feature: trim whitespace when there are only template children
g105b Jan 17, 2023
a28c12c
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 17, 2023
da35c48
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 23, 2023
b2f8fa5
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 26, 2023
a3cced8
feature: handle vars section in partial comment ini
g105b Jan 30, 2023
0837972
tweak: nullable call
g105b Jan 30, 2023
ea28a6d
tweak: stan improvement
g105b Jan 30, 2023
800513a
Merge branch 'master' into 400-vars
g105b Jan 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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