Skip to content

Commit dfbf311

Browse files
authored
Update examples, include another for binding nested callbacks (#370)
* build: upgrade dom requirement and loosen version range * docs: update examples
1 parent 2c22ea0 commit dfbf311

10 files changed

+303
-35
lines changed

examples/binding/01-simple.php

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding#simplest-possible-usage
88

99
$html = <<<HTML
10+
<!DOCTYPE html>
1011
<h1 data-bind:text>Name</h1>
1112
HTML;
1213

@@ -21,3 +22,12 @@ function example(DocumentBinder $binder):void {
2122
example($binder);
2223
$binder->cleanDatasets();
2324
echo $document;
25+
26+
/* Output:
27+
<!DOCTYPE html>
28+
<html>
29+
<body>
30+
<h1>Cody</h1>
31+
</body>
32+
</html>
33+
*/

examples/binding/02-non-trivial.php

+46
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding#non-trivial-usage
1111

1212
$html = <<<HTML
13+
<!DOCTYPE html>
1314
<h1>Top three drivers of <span data-bind:text="year">0000</span></h1>
1415
1516
<ul>
@@ -50,6 +51,51 @@ function example(DocumentBinder $binder, DriverRepository $driverRepo):void {
5051
$binder->cleanDatasets();
5152
echo $document;
5253

54+
/* Output:
55+
<!DOCTYPE html>
56+
<html>
57+
<body>
58+
<h1>Top three drivers of <span>2022</span></h1>
59+
60+
<ul id="template-parent-62fe4594d5666">
61+
<li>
62+
<h2>Lewis Hamilton</h2>
63+
<h3>Mercedes</h3>
64+
65+
<p>Points: <span>347</span>
66+
</p>
67+
<div>
68+
<img src="/flag/GBR.png" alt="Flag of United Kingdom">
69+
<p>United Kingdom</p>
70+
</div>
71+
</li>
72+
<li>
73+
<h2>Valtteri Bottas</h2>
74+
<h3>Mercedes</h3>
75+
76+
<p>Points: <span>223</span>
77+
</p>
78+
<div>
79+
<img src="/flag/FIN.png" alt="Flag of Finland">
80+
<p>Finland</p>
81+
</div>
82+
</li>
83+
<li>
84+
<h2>Max Verstappen</h2>
85+
<h3>Red Bull Racing Honda</h3>
86+
87+
<p>Points: <span>214</span>
88+
</p>
89+
<div>
90+
<img src="/flag/NED.png" alt="Flag of Netherlands">
91+
<p>Netherlands</p>
92+
</div>
93+
</li>
94+
</ul>
95+
</body>
96+
</html>
97+
*/
98+
5399
class DriverRepository {
54100
/** @return array<Driver>
55101
* @noinspection PhpUnusedParameterInspection

examples/binding/03-bindValue.php

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding#bindvalue
88

99
$html = <<<HTML
10+
<!DOCTYPE html>
1011
<p data-bind:text>This is a quick example</p>
1112
HTML;
1213

@@ -21,3 +22,11 @@ function example(DocumentBinder $binder):void {
2122
example($binder);
2223
$binder->cleanDatasets();
2324
echo $document;
25+
26+
/* Output:
27+
<!DOCTYPE html>
28+
<html>
29+
<body>
30+
<p>This is an updated example</p>
31+
</body>
32+
</html>

examples/binding/04-bindKeyValue.php

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding#bindkeyvalue
88

99
$html = <<<HTML
10+
<!doctype html>
1011
<h1>Hello, <span data-bind:text="name">you</span>!</h1>
1112
HTML;
1213

@@ -21,3 +22,12 @@ function example(DocumentBinder $binder):void {
2122
example($binder);
2223
$binder->cleanDatasets();
2324
echo $document;
25+
26+
/* Output:
27+
<!DOCTYPE html>
28+
<html>
29+
<body>
30+
<h1>Hello, <span>Cody</span>!</h1>
31+
</body>
32+
</html>
33+
*/

examples/binding/05-bindData.php

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding#bindkeyvalue
88

99
$html = <<<HTML
10+
<!doctype html>
1011
<h1>User profile</h1>
1112
1213
<div>
@@ -35,3 +36,19 @@ function example(DocumentBinder $binder):void {
3536
example($binder);
3637
$binder->cleanDatasets();
3738
echo $document;
39+
40+
/* Output:
41+
<!DOCTYPE html>
42+
<html>
43+
<body>
44+
<h1>User profile</h1>
45+
46+
<div>
47+
<h2>PhpNut</h2>
48+
<p>Full name: <span>Larry E. Masters</span></p>
49+
<p>Bio: <span>Christian - Dad - 4x Grandad - Co-Founder of @CakePHP - Developer - Open Source Advocate</span></p>
50+
</div>
51+
</body>
52+
53+
</html>
54+
*/

examples/binding/06-bindList.php

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding#bindlist
88

99
$html = <<<HTML
10+
<!doctype html>
1011
<h1>Shopping list</h1>
1112
1213
<ul>
@@ -31,3 +32,20 @@ function example(DocumentBinder $binder):void {
3132
example($binder);
3233
$binder->cleanDatasets();
3334
echo $document;
35+
36+
/* Output:
37+
<!DOCTYPE html>
38+
<html>
39+
<body>
40+
<h1>Shopping list</h1>
41+
42+
<ul id="template-parent-62fe45171c32e">
43+
44+
<li>Eggs</li>
45+
<li>Potatoes</li>
46+
<li>Butter</li>
47+
<li>Plain flour</li>
48+
</ul>
49+
</body>
50+
</html>
51+
*/

examples/binding/07-bindListCallback.php

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding#bindlistcallback
99

1010
$html = <<<HTML
11+
<!doctype html>
1112
<h1>Shopping list</h1>
1213
1314
<ul>
@@ -35,3 +36,20 @@ function example(DocumentBinder $binder):void {
3536
example($binder);
3637
$binder->cleanDatasets();
3738
echo $document;
39+
40+
/* Output:
41+
<!DOCTYPE html>
42+
<html>
43+
<body>
44+
<h1>Shopping list</h1>
45+
46+
<ul id="template-parent-62fe44ffc92e0">
47+
<li class="item-0">Eggs (item 0)</li>
48+
<li class="item-1">Potatoes (item 1)</li>
49+
<li class="item-2">Butter (item 2)</li>
50+
<li class="item-3">Plain flour (item 3)</li>
51+
</ul>
52+
</body>
53+
54+
</html>
55+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
use Gt\Dom\Element;
3+
use Gt\Dom\HTMLDocument;
4+
use Gt\DomTemplate\DocumentBinder;
5+
6+
require __DIR__ . "/../../vendor/autoload.php";
7+
8+
// EXAMPLE CODE: https://github.com/PhpGt/DomTemplate/wiki/Binding#bindlistcallback
9+
10+
$html = <<<HTML
11+
<!doctype html>
12+
<h1>Menu</h1>
13+
14+
<ul>
15+
<li data-template>
16+
<h2 data-bind:text="title">Menu item title</h2>
17+
<p>Ingredients:</p>
18+
<ul>
19+
<li data-template data-bind:text>Ingredient goes here</li>
20+
</ul>
21+
</li>
22+
</ul>
23+
HTML;
24+
25+
function example(DocumentBinder $binder):void {
26+
$listData = [
27+
[
28+
"title" => "Roast king oyster mushroom",
29+
"ingredients" => ["hazelnut", "summer truffle", "black garlic"],
30+
],
31+
[
32+
"title" => "Cornish skate wing",
33+
"ingredients" => ["borlotti cassoulet", "lilliput caper", "baby gem", "orange oil"],
34+
],
35+
[
36+
"title" => "Aged Derbyshire beef",
37+
"ingredients" => ["turnip", "pickled mustard", "bone marrow mash", "rainbow chard"],
38+
],
39+
];
40+
$binder->bindListCallback($listData, function(Element $element, $listItem, $listKey) use($binder) {
41+
$binder->bindKeyValue("title", $listItem["title"]);
42+
$binder->bindList($listItem["ingredients"], $element);
43+
});
44+
}
45+
46+
// END OF EXAMPLE CODE.
47+
48+
$document = new HTMLDocument($html);
49+
$binder = new DocumentBinder($document);
50+
example($binder);
51+
$binder->cleanDatasets();
52+
echo $document;
53+
54+
/* Output:
55+
<!DOCTYPE html>
56+
<html>
57+
<body>
58+
<h1>Menu</h1>
59+
60+
<ul id="template-parent-62fe445401332">
61+
<li>
62+
<h2>Roast king oyster mushroom</h2>
63+
<p>Ingredients:</p>
64+
<ul id="template-parent-62fe44540144e">
65+
<li>hazelnut</li>
66+
<li>summer truffle</li>
67+
<li>black garlic</li>
68+
</ul>
69+
</li>
70+
<li>
71+
<h2>Cornish skate wing</h2>
72+
<p>Ingredients:</p>
73+
<ul id="template-parent-62fe44540144e">
74+
<li>borlotti cassoulet</li>
75+
<li>lilliput caper</li>
76+
<li>baby gem</li>
77+
<li>orange oil</li>
78+
</ul>
79+
</li>
80+
<li>
81+
<h2>Aged Derbyshire beef</h2>
82+
<p>Ingredients:</p>
83+
<ul id="template-parent-62fe44540144e">
84+
<li>turnip</li>
85+
<li>pickled mustard</li>
86+
<li>bone marrow mash</li>
87+
<li>rainbow chard</li>
88+
</ul>
89+
</li>
90+
</ul>
91+
</body>
92+
</html>
93+
*/

examples/binding/08-bindTable.php

-35
This file was deleted.

0 commit comments

Comments
 (0)