diff --git a/examples/partial/01-basic-template.php b/examples/partial/01-basic-template.php
new file mode 100644
index 0000000..607a728
--- /dev/null
+++ b/examples/partial/01-basic-template.php
@@ -0,0 +1,62 @@
+
+
+
+
+ My website :: {{site-heading ?? Home}}
+
+
+ My website!
+
+
+
+
+
+
+HTML;
+
+$pageAbout = <<
+
+About me
+This is my about me page on my amazing website!
+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());
+}
diff --git a/src/CommentIni.php b/src/CommentIni.php
index d0a1c4d..94493ca 100644
--- a/src/CommentIni.php
+++ b/src/CommentIni.php
@@ -73,6 +73,11 @@ public function get(string $variable):?string {
return $var;
}
+ /** @return array */
+ public function getVars():array {
+ return $this->iniData["vars"] ?? [];
+ }
+
public function containsIniData():bool {
return !empty($this->iniData);
}
diff --git a/src/PartialExpander.php b/src/PartialExpander.php
index 45728f0..920725b 100644
--- a/src/PartialExpander.php
+++ b/src/PartialExpander.php
@@ -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 $partialDocumentArray */
$partialDocumentArray = [];
do {
@@ -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.");
@@ -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");
@@ -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);
}
}
diff --git a/test/phpunit/PartialContentTest.php b/test/phpunit/PartialContentTest.php
index 7a3d2e8..d9e0011 100644
--- a/test/phpunit/PartialContentTest.php
+++ b/test/phpunit/PartialContentTest.php
@@ -1,9 +1,11 @@
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);