|
| 1 | +# Dataplater |
| 2 | + |
| 3 | +HTML template engine that uses data-attributes and keeps HTML templates valid and clean. Scroll down to see a usage example. |
| 4 | + |
| 5 | +## Install |
| 6 | +``` |
| 7 | +composer require leongrdic/dataplater |
| 8 | +``` |
| 9 | + |
| 10 | +### Requirements |
| 11 | + |
| 12 | +- XML extension |
| 13 | +- DOM extension |
| 14 | + |
| 15 | + |
| 16 | +## Attributes |
| 17 | + |
| 18 | +| **attribute name** | **attribute value** | result | |
| 19 | +|-----------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------| |
| 20 | +| `data-var-[text / html]` | `value expression` | inserts or replaces (html) content with supplied value | |
| 21 | +| `data-var-[id / class / title / alt / value / href / src / style]` | `value expression` | inserts or replaces attribute value with supplied value | |
| 22 | +| `data-var-attr` | `attribute name ; value expression` | adds or replaces attribute value for a specified attribute | |
| 23 | +| `data-var-foreach` | `expression to iterate through ; variable name for value`<br/>or<br/>`expression to iterate through ; variable name for key ; variable name for value` | iterates through var and writes value and/or key variables | |
| 24 | +| `data-var-if` | `condition expression` | removes block if condition not true | |
| 25 | +| `data-var-if-[text/html]` | `condition expression ; value expression if true`<br/>or<br/>`condition expression ; value expression if true ; value expression if false` | inserts or replaces (html) content with value expression depending on the condition expressions result | |
| 26 | +| `data-var-if-[id / class / title / alt / value / href / src / style]` | `condition expression ; value expression if true`<br/>or<br/>`condition expression ; value expression if true ; value expression if false` | adds or replaces attribute value for the attribute depending on the condition expressions result | |
| 27 | +| `data-var-if-attr` | `attribute name ; condition expression ; value expression if true`<br/>or<br/>`attribute name ; condition expression ; value expression if true ; value expression if false` | adds or replaces attribute value for a specified attribute name depending on the condition expression result | |
| 28 | + |
| 29 | + |
| 30 | +## Expression syntax: |
| 31 | + |
| 32 | +### `variableName` |
| 33 | +returns content of the variable |
| 34 | + |
| 35 | +if variable is a closure, executes it and returns the result |
| 36 | + |
| 37 | +variable names shouldn't contain characters like dots or spaces, but ideally they should only contain letters, numbers, `_` and `-` |
| 38 | + |
| 39 | +### `variableName.arrayElement.objectProperty` |
| 40 | +access element of array or object property when array/object is in variable |
| 41 | + |
| 42 | +also supports closures at any depth |
| 43 | + |
| 44 | +### `v/json` |
| 45 | +decodes json after `/`, useful for literal values like: `v/true`, `v/false`, `v/0` or even strings, e.g. `v/"string"` |
| 46 | + |
| 47 | +### `f/variableName, param, ...` |
| 48 | +calls the callable in `variableName` and passed provided params (if any) |
| 49 | + |
| 50 | +params can be encapsulated into `|` characters if your expression contains `,` |
| 51 | + |
| 52 | + |
| 53 | +## Built-in functions |
| 54 | + |
| 55 | +| **function** | **php equivalent** | **notes** | |
| 56 | +|------------------------------------------|----------------------------------------|----------------------------------------| |
| 57 | +| `logic.and, a, b` | `$a && $b` | | |
| 58 | +| `logic.or, a, b` | `$a \|\| $b` | | |
| 59 | +| `logic.not, a` | `!$a` | | |
| 60 | +| `compare.equal, a, b` | `$a == $b` | | |
| 61 | +| `compare.different, a, b` | `$a != $b` | | |
| 62 | +| `compare.exact, a, b` | `$a === $b` | | |
| 63 | +| `compare.notExact, a, b` | `$a !== $b` | | |
| 64 | +| `compare.larger, a, b` | `$a > $b` | | |
| 65 | +| `compare.largerEqual, a, b` | `$a >= $b` | | |
| 66 | +| `compare.smaller, a, b` | `$a < $b` | | |
| 67 | +| `compare.smallerEqual, a, b` | `$a <= $b` | | |
| 68 | +| `count, var` | `count($var)` | var can be a countable array or object | |
| 69 | +| `text.concat, string1, string2, ...` | `implode('', $strings)` | | |
| 70 | +| `text.implode, separator, array` | `implode($separator, $array)` | | |
| 71 | +| `text.explode, separator, string, limit` | `explode($separator, $string, $limit)` | limit is optional | |
| 72 | +| `json.decode, string` | `json_decode($string, true)` | | |
| 73 | +| `json.encode, var` | `json_encode($var)` | | |
| 74 | + |
| 75 | +## Example |
| 76 | + |
| 77 | +PHP: |
| 78 | +```php |
| 79 | +<?php |
| 80 | +require_once 'vendor/autoload.php'; |
| 81 | + |
| 82 | +echo (new \Le\Dataplater('template.html', [ |
| 83 | + 'users' => [ |
| 84 | + ['Demo', 'User', '01.01.2020.'], |
| 85 | + ['John', 'Doe', '31.12.2021.'], |
| 86 | + ], |
| 87 | + 'links' => [ |
| 88 | + ['name' => 'Google', 'url' => 'https://google.com/'], |
| 89 | + ['name' => 'YouTube', 'url' => 'https://youtube.com/'], |
| 90 | + ['name' => 'Facebook', 'url' => 'https://facebook.com/'] |
| 91 | + ], |
| 92 | + 'items' => [ |
| 93 | + 'first', |
| 94 | + 'second', |
| 95 | + 'third', |
| 96 | + 'fourth' |
| 97 | + ], |
| 98 | + 'balance' => 10, |
| 99 | + 'sentence' => 'split this sentence by spaces', |
| 100 | + 'lang' => function(){ |
| 101 | + return [ |
| 102 | + 'balance_empty' => 'Your balance is empty.', |
| 103 | + 'some_text' => 'Translation' |
| 104 | + ]; |
| 105 | + } |
| 106 | +]))->render(); |
| 107 | +``` |
| 108 | + |
| 109 | +`template.html`: |
| 110 | +```html |
| 111 | +<table> |
| 112 | + <template data-var-foreach="users; row"> |
| 113 | + <tr> |
| 114 | + <template data-var-foreach="row; cell"> |
| 115 | + <td data-var-text="cell"></td> |
| 116 | + </template> |
| 117 | + </tr> |
| 118 | + </template> |
| 119 | +</table> |
| 120 | +<span>table has <var data-var-text="f/count, users"></var> rows</span> |
| 121 | + |
| 122 | +<div> |
| 123 | + <template data-var-foreach="links; index; link"> |
| 124 | + <a data-var-href="link.url" data-var-text='f/text.concat, index, v/" ", link.name'></a><br> |
| 125 | + </template> |
| 126 | +</div> |
| 127 | + |
| 128 | +<div data-var-if-text='f/compare.equal, balance, v/0 ; lang.balance_empty'>Your current balance is <var data-var-text="balance"></var> dollars</div> |
| 129 | + |
| 130 | +<ul> |
| 131 | + <template data-var-foreach='f/text.explode, v/" ", sentence, v/4 ; word'> |
| 132 | + <li data-var-text="word"></li> |
| 133 | + </template> |
| 134 | +</ul> |
| 135 | +``` |
| 136 | + |
| 137 | +output: |
| 138 | +```html |
| 139 | +<table> |
| 140 | + <tr> |
| 141 | + <td>Demo</td> |
| 142 | + <td>User</td> |
| 143 | + <td>01.01.2020.</td> |
| 144 | + </tr> |
| 145 | + <tr> |
| 146 | + <td>John</td> |
| 147 | + <td>Doe</td> |
| 148 | + <td>31.12.2021.</td> |
| 149 | + </tr> |
| 150 | +</table> |
| 151 | +<span>table has <var>2</var> rows</span> |
| 152 | + |
| 153 | +<div> |
| 154 | + <a href="https://google.com/">0 Google</a><br> |
| 155 | + <a href="https://youtube.com/">1 YouTube</a><br> |
| 156 | + <a href="https://facebook.com/">2 Facebook</a><br> |
| 157 | +</div> |
| 158 | + |
| 159 | +<div>Your current balance is <var>10</var> dollars</div> |
| 160 | + |
| 161 | +<ul> |
| 162 | + <li>split</li> |
| 163 | + <li>this</li> |
| 164 | + <li>sentence</li> |
| 165 | + <li>by spaces</li> |
| 166 | +</ul> |
| 167 | +``` |
| 168 | +(not as nicely formated as here) |
| 169 | + |
| 170 | +## Disclaimer |
| 171 | + |
| 172 | +Use on your own responsibility, this may not be suitable for production - I honestly haven't benchmarked it. |
0 commit comments