|
| 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 | + */ |
0 commit comments