Skip to content

Commit 151ed5f

Browse files
authored
Bind keys of kvp data using _key parameter (#143)
1 parent eaef00d commit 151ed5f

File tree

4 files changed

+123
-59
lines changed

4 files changed

+123
-59
lines changed

composer.lock

+80-58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Bindable.php

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function bindList(
137137

138138
if(is_string($key)) {
139139
$insertedT->bindValue($key);
140+
$insertedT->bindKeyValue("_key", $key);
140141
}
141142

142143
if($this->isBindableValue($data)) {

test/unit/BindableTest.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php /** @noinspection PhpComposerExtensionStubsInspection */
2-
32
namespace Gt\DomTemplate\Test;
43

54
use EmptyIterator;
@@ -659,4 +658,27 @@ public function testBindListEmptySetsInnerHtmlToEmpty() {
659658
$ul->bindList([]);
660659
self::assertEquals("", $ul->innerHTML);
661660
}
661+
662+
public function testBindKvpList() {
663+
$kvpData = [
664+
"Name" => "Alan",
665+
"Occupation" => "Cryptanalyst",
666+
"Place of work" => "Bletchley Park",
667+
];
668+
669+
$document = new HTMLDocument(Helper::HTML_KVP_LIST);
670+
$document->extractTemplates();
671+
$ul = $document->getElementById("list");
672+
$ul->bindList($kvpData);
673+
674+
$expectedKeys = array_keys($kvpData);
675+
$expectedValues = array_values($kvpData);
676+
foreach($ul->querySelectorAll("li") as $i => $li) {
677+
$keySpan = $li->querySelector(".key span");
678+
$valueSpan = $li->querySelector(".value span");
679+
680+
self::assertEquals($expectedKeys[$i], $keySpan->textContent);
681+
self::assertEquals($expectedValues[$i], $valueSpan->textContent);
682+
}
683+
}
662684
}

test/unit/Helper/Helper.php

+19
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,25 @@ class Helper {
567567
]
568568
];
569569

570+
const HTML_KVP_LIST = <<<HTML
571+
<!doctype html>
572+
<meta charset="utf-8" />
573+
<title>KVP list test</title>
574+
<main>
575+
<ul id="list">
576+
<li data-template>
577+
<p class="key">
578+
KEY: <span data-bind:text="_key">Key goes here</span>
579+
</p>
580+
<p class="value">
581+
VALUE: <span data-bind:text>Value goes here</span>
582+
</p>
583+
</li>
584+
</ul>
585+
</main>
586+
HTML;
587+
588+
570589
const HTML_SHOP = <<<HTML
571590
<!doctype html>
572591
<meta charset="utf-8" />

0 commit comments

Comments
 (0)