|
| 1 | +# DVE Grammar |
| 2 | + |
| 3 | +Quick reference for Deserve `.dve` template syntax. |
| 4 | + |
| 5 | +- **Editor tooling overview**: See [`editor/README.md`](../README.md) |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +- [DVE Scope Mapping](#dve-scope-mapping) |
| 10 | +- [Syntax Overview](#syntax-overview) |
| 11 | +- [Variables](#variables) |
| 12 | +- [Raw Output (Unescaped)](#raw-output-unescaped) |
| 13 | +- [Include](#include) |
| 14 | +- [If / Else](#if--else) |
| 15 | +- [Each](#each) |
| 16 | +- [Each Metadata](#each-metadata) |
| 17 | +- [Expressions](#expressions) |
| 18 | +- [Advanced Examples](#advanced-examples) |
| 19 | +- [Escaping Rules](#escaping-rules) |
| 20 | + |
| 21 | +## DVE Scope Mapping |
| 22 | + |
| 23 | +| Syntax | Scope | |
| 24 | +| ---------------------------------- | ------------------------------------------------------- | |
| 25 | +| `{{` `}}` `{{{` `}}}` | `meta.tag.dve` / `meta.tag.raw.dve` | |
| 26 | +| `#if` `#each` `else` `/if` `/each` | `keyword.control.dve` | |
| 27 | +| `>` (include) | `keyword.control.include.dve` | |
| 28 | +| `as` (in #each) | `keyword.operator.as.dve` | |
| 29 | +| Include path | `string.unquoted.path.dve` | |
| 30 | +| `true` `false` `null` `undefined` | `constant.language.dve` | |
| 31 | +| Numbers | `constant.numeric.dve` | |
| 32 | +| `"..."` `'...'` | `string.quoted.double.dve` / `string.quoted.single.dve` | |
| 33 | +| Operators `?.` `===` `??` etc. | `keyword.operator.dve` | |
| 34 | +| Identifiers / variables | `variable.other.dve` | |
| 35 | +| Item name in #each | `variable.parameter.dve` | |
| 36 | + |
| 37 | +## Syntax Overview |
| 38 | + |
| 39 | +DVE uses `{{ ... }}` for expressions and `{{#...}} ... {{/...}}` for blocks. |
| 40 | + |
| 41 | +```txt |
| 42 | +Hello {{ user?.name ?? 'Guest' }}. |
| 43 | +{{#if user?.isAdmin}}ADMIN{{else}}USER{{/if}} |
| 44 | +``` |
| 45 | + |
| 46 | +## Variables |
| 47 | + |
| 48 | +Use `{{ value }}` to print a value. Output is escaped by default. |
| 49 | + |
| 50 | +```txt |
| 51 | +Hello {{ name }}. |
| 52 | +``` |
| 53 | + |
| 54 | +## Raw Output (Unescaped) |
| 55 | + |
| 56 | +Use triple braces to skip escaping. |
| 57 | + |
| 58 | +```txt |
| 59 | +{{{ html }}} |
| 60 | +``` |
| 61 | + |
| 62 | +## Include |
| 63 | + |
| 64 | +Include another template using `{{> path }}`. The path is relative to `viewsDir`. |
| 65 | + |
| 66 | +```txt |
| 67 | +{{> partials/header.dve}} |
| 68 | +``` |
| 69 | + |
| 70 | +## If / Else |
| 71 | + |
| 72 | +Inline if/else: |
| 73 | + |
| 74 | +```txt |
| 75 | +{{#if ok}}YES{{else}}NO{{/if}} |
| 76 | +``` |
| 77 | + |
| 78 | +## Each |
| 79 | + |
| 80 | +Loop an array with `#each`. |
| 81 | + |
| 82 | +```txt |
| 83 | +{{#each items as item}}{{ item }},{{/each}} |
| 84 | +``` |
| 85 | + |
| 86 | +## Each Metadata |
| 87 | + |
| 88 | +Inside `#each`, you can use: |
| 89 | + |
| 90 | +- `@index` |
| 91 | +- `@first` |
| 92 | +- `@last` |
| 93 | +- `@length` |
| 94 | + |
| 95 | +```txt |
| 96 | +{{#each items as item}}({{ @index }}/{{ @length }} {{#if @first}}F{{else}}-{{/if}}{{#if @last}}L{{else}}-{{/if}}={{ item }});{{/each}} |
| 97 | +``` |
| 98 | + |
| 99 | +## Expressions |
| 100 | + |
| 101 | +DVE supports JS-like expressions for lookups and basic operators. |
| 102 | + |
| 103 | +```txt |
| 104 | +Hello {{ user?.name ?? 'Guest' }}. |
| 105 | +Sum={{ 1 + 2 * 3 }} |
| 106 | +``` |
| 107 | + |
| 108 | +## Advanced Examples |
| 109 | + |
| 110 | +### Layout + Partial Composition |
| 111 | + |
| 112 | +`views/layout.dve`: |
| 113 | + |
| 114 | +```txt |
| 115 | +<html> |
| 116 | + <body> |
| 117 | + {{> partials/header.dve}} |
| 118 | + <main> |
| 119 | + {{{ bodyHtml }}} |
| 120 | + </main> |
| 121 | + {{> partials/footer.dve}} |
| 122 | + </body> |
| 123 | +</html> |
| 124 | +``` |
| 125 | + |
| 126 | +`views/partials/header.dve`: |
| 127 | + |
| 128 | +```txt |
| 129 | +<header> |
| 130 | + <h1>{{ title ?? 'Untitled' }}</h1> |
| 131 | + {{#if user?.name}}<p>Hello {{ user.name }}.</p>{{else}}<p>Hello Guest.</p>{{/if}} |
| 132 | +</header> |
| 133 | +``` |
| 134 | + |
| 135 | +### Lists With Conditional Blocks |
| 136 | + |
| 137 | +```txt |
| 138 | +{{#if items?.length ?? 0}} |
| 139 | + <ul> |
| 140 | + {{#each items as item}} |
| 141 | + <li> |
| 142 | + {{#if item?.isPinned}}[PIN] {{/if}} |
| 143 | + ({{ @index + 1 }}/{{ @length }}) {{ item?.label ?? 'No label' }} |
| 144 | + </li> |
| 145 | + {{/each}} |
| 146 | + </ul> |
| 147 | +{{else}} |
| 148 | + <p>No items.</p> |
| 149 | +{{/if}} |
| 150 | +``` |
| 151 | + |
| 152 | +### Nested Each (Matrix-Style) |
| 153 | + |
| 154 | +```txt |
| 155 | +<table> |
| 156 | + {{#each rows as row}} |
| 157 | + <tr> |
| 158 | + {{#each row as cell}} |
| 159 | + <td>{{ cell }}</td> |
| 160 | + {{/each}} |
| 161 | + </tr> |
| 162 | + {{/each}} |
| 163 | +</table> |
| 164 | +``` |
| 165 | + |
| 166 | +### Safe Vs Raw Output |
| 167 | + |
| 168 | +```txt |
| 169 | +Escaped: {{ userInput }} |
| 170 | +Raw: {{{ trustedHtml }}} |
| 171 | +``` |
| 172 | + |
| 173 | +## Escaping Rules |
| 174 | + |
| 175 | +- `{{ value }}` escapes HTML by default |
| 176 | +- `{{{ value }}}` outputs raw HTML (no escaping) |
0 commit comments