rustango-cms 0.2.0

A page-tree content management system for Rust, built on rustango
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
{# =====================================================================
   `_widget.html` — single source of truth for rendering a `Widget` as
   an HTML form input. Both the flat extension-field surface
   (`page_form.html`) and (in a later slice) the stream-block editor
   call into this macro so widget output is consistent everywhere.

   Multi-value contract: `Checkboxes` / `MultiSelect` widgets expect
   the CLIENT to JSON-encode the selected values into a single form
   field before POST (`<input name="opts" value='["a","b"]'>`). The
   server-side `axum::Form<HashMap<String,String>>` extractor would
   otherwise drop duplicate keys. The bundled JS module
   (Phase 4, lands later) handles this transparently.

   Macro: `render(w)` — `w` is a `Widget`-shaped struct/JSON value:
     { name, kind, label, value, options, help, placeholder,
       required, min, max, step, max_length, read_only }
   ===================================================================== #}

{% macro render(w) -%}
<div class="rcms-field" data-widget="{{ w.kind }}">
    {% if w.kind != "hidden" %}
        <label for="w_{{ w.name }}">
            {{ w.label }}
            {% if w.required %}<span class="rcms-req">*</span>{% endif %}
        </label>
    {% endif %}

    {# ---- text-shaped ------------------------------------------------ #}
    {% if w.kind == "text" %}
        <input type="text" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.placeholder %}placeholder="{{ w.placeholder }}"{% endif %}
               {% if w.max_length %}maxlength="{{ w.max_length }}"{% endif %}
               {% if w.min_length %}minlength="{{ w.min_length }}"{% endif %}
               {% if w.pattern %}pattern="{{ w.pattern }}"{% endif %}
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "textarea" %}
        <textarea id="w_{{ w.name }}" name="{{ w.name }}" rows="6"
                  {% if w.placeholder %}placeholder="{{ w.placeholder }}"{% endif %}
                  {% if w.max_length %}maxlength="{{ w.max_length }}"{% endif %}
                  {% if w.min_length %}minlength="{{ w.min_length }}"{% endif %}
                  {% if w.required %}required{% endif %}
                  {% if w.read_only %}readonly{% endif %}>{{ w.value | default(value='') }}</textarea>

    {% elif w.kind == "markdown" %}
        {# #205 — markdown toolbar. Buttons insert markdown syntax
           at the cursor via cms-ux.js (progressive enhancement;
           plain textarea fallback works without JS). #}
        <div class="rcms-md-toolbar" data-md-toolbar-for="w_{{ w.name }}" hidden>
            <button type="button" class="rcms-md-btn" data-md-action="bold" title="{{ "Bold (Cmd+B)" | translate(locale=LANG) }}"><strong>B</strong></button>
            <button type="button" class="rcms-md-btn" data-md-action="italic" title="{{ "Italic (Cmd+I)" | translate(locale=LANG) }}"><em>I</em></button>
            <span class="rcms-md-sep"></span>
            <button type="button" class="rcms-md-btn" data-md-action="h2" title="{{ "Heading 2" | translate(locale=LANG) }}">H2</button>
            <button type="button" class="rcms-md-btn" data-md-action="h3" title="{{ "Heading 3" | translate(locale=LANG) }}">H3</button>
            <span class="rcms-md-sep"></span>
            <button type="button" class="rcms-md-btn" data-md-action="link" title="{{ "Link (Cmd+K)" | translate(locale=LANG) }}">🔗</button>
            <button type="button" class="rcms-md-btn" data-md-action="ul" title="{{ "Bulleted list" | translate(locale=LANG) }}">• {{ "List" | translate(locale=LANG) }}</button>
            <button type="button" class="rcms-md-btn" data-md-action="ol" title="{{ "Numbered list" | translate(locale=LANG) }}">1. {{ "List" | translate(locale=LANG) }}</button>
            <span class="rcms-md-sep"></span>
            <button type="button" class="rcms-md-btn" data-md-action="quote" title="{{ "Quote" | translate(locale=LANG) }}">❝</button>
            <button type="button" class="rcms-md-btn" data-md-action="code" title="{{ "Inline code" | translate(locale=LANG) }}">&lt;/&gt;</button>
            <button type="button" class="rcms-md-btn" data-md-action="codeblock" title="{{ "Code block" | translate(locale=LANG) }}">{ }</button>
        </div>
        <textarea id="w_{{ w.name }}" name="{{ w.name }}" rows="14"
                  class="rcms-text-sm rcms-font-mono"
                  data-widget-mode="markdown"
                  {% if w.required %}required{% endif %}
                  {% if w.read_only %}readonly{% endif %}>{{ w.value | default(value='') }}</textarea>

    {% elif w.kind == "richtext" %}
        {# richtext-editor.js swaps this textarea for the TipTap editor,
           which brings its own toolbar; the textarea stays as the
           submitted value and the no-JS fallback. #}
        <textarea id="w_{{ w.name }}" name="{{ w.name }}" rows="10"
                  data-widget-mode="richtext"
                  {% if w.required %}required{% endif %}
                  {% if w.read_only %}readonly{% endif %}>{{ w.value | default(value='') }}</textarea>

    {% elif w.kind == "email" %}
        <input type="email" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.placeholder %}placeholder="{{ w.placeholder }}"{% endif %}
               {% if w.max_length %}maxlength="{{ w.max_length }}"{% endif %}
               {% if w.min_length %}minlength="{{ w.min_length }}"{% endif %}
               {% if w.pattern %}pattern="{{ w.pattern }}"{% endif %}
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "url" %}
        <input type="url" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.placeholder %}placeholder="{{ w.placeholder }}"{% endif %}
               {% if w.max_length %}maxlength="{{ w.max_length }}"{% endif %}
               {% if w.min_length %}minlength="{{ w.min_length }}"{% endif %}
               {% if w.pattern %}pattern="{{ w.pattern }}"{% endif %}
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "tel" %}
        <input type="tel" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.placeholder %}placeholder="{{ w.placeholder }}"{% endif %}
               {% if w.max_length %}maxlength="{{ w.max_length }}"{% endif %}
               {% if w.min_length %}minlength="{{ w.min_length }}"{% endif %}
               {% if w.pattern %}pattern="{{ w.pattern }}"{% endif %}
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "password" %}
        <input type="password" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.max_length %}maxlength="{{ w.max_length }}"{% endif %}
               {% if w.min_length %}minlength="{{ w.min_length }}"{% endif %}
               {% if w.pattern %}pattern="{{ w.pattern }}"{% endif %}
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "hidden" %}
        <input type="hidden" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}">

    {# ---- numeric --------------------------------------------------- #}
    {% elif w.kind == "number" or w.kind == "integer" or w.kind == "float" %}
        <input type="number" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.placeholder %}placeholder="{{ w.placeholder }}"{% endif %}
               {% if w.min is defined and w.min is number %}min="{{ w.min }}"{% endif %}
               {% if w.max is defined and w.max is number %}max="{{ w.max }}"{% endif %}
               {% if w.step %}step="{{ w.step }}"{% elif w.kind == "integer" %}step="1"{% elif w.kind == "float" %}step="any"{% endif %}
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "range" %}
        <input type="range" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.min is defined and w.min is number %}min="{{ w.min }}"{% endif %}
               {% if w.max is defined and w.max is number %}max="{{ w.max }}"{% endif %}
               {% if w.step %}step="{{ w.step }}"{% endif %}
               {% if w.read_only %}disabled{% endif %}>

    {# ---- boolean (single checkbox) -------------------------------- #}
    {% elif w.kind == "boolean" %}
        {# Phase 7 — SwitchInput. `variant="switch"`
           flips the checkbox to an iOS-style toggle while keeping
           the same wire shape (checkbox `on` / absent). #}
        <div class="rcms-checkbox-field{% if w.variant == "switch" %} checkbox-field--switch{% endif %}">
            <input type="checkbox" id="w_{{ w.name }}" name="{{ w.name }}"
                   value="on"
                   {% if w.value == "on" or w.value == "true" or w.value == "1" %}checked{% endif %}
                   {% if w.read_only %}disabled{% endif %}>
            {% if w.variant == "switch" %}
                <label for="w_{{ w.name }}" class="rcms-switch-track" aria-hidden="true">
                    <span class="rcms-switch-knob"></span>
                </label>
            {% endif %}
            {% if w.help %}<span class="rcms-hint">{{ w.help }}</span>{% endif %}
        </div>

    {# ---- date / time ---------------------------------------------- #}
    {% elif w.kind == "date" %}
        <input type="date" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "time" %}
        <input type="time" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.step %}step="{{ w.step }}"{% endif %}
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "datetime" %}
        <input type="datetime-local" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{{ w.value | default(value='') }}"
               {% if w.step %}step="{{ w.step }}"{% endif %}
               {% if w.required %}required{% endif %}
               {% if w.read_only %}readonly{% endif %}>

    {% elif w.kind == "datetimetz" %}
        {# Datetime + timezone pair. Value shape:
              `YYYY-MM-DDTHH:MM[:SS][Z|+HH:MM]`
           The client packs the picker's local datetime + the picked
           IANA zone into an ISO-8601 string before submit. Until JS
           enhancement lands, both inputs POST under suffixed names
           the server-side handler reassembles. #}
        <div class="rcms-datetimetz-pair">
            <input type="datetime-local" id="w_{{ w.name }}" name="{{ w.name }}__local"
                   value="{{ w.value | default(value='') | split(pat='@') | nth(n=0) }}"
                   {% if w.required %}required{% endif %}
                   {% if w.read_only %}readonly{% endif %}>
            <select id="w_{{ w.name }}_tz" name="{{ w.name }}__tz"
                    {% if w.read_only %}disabled{% endif %}>
                <option value="UTC">UTC</option>
                <option value="America/New_York">America/New_York</option>
                <option value="America/Los_Angeles">America/Los_Angeles</option>
                <option value="Europe/London">Europe/London</option>
                <option value="Europe/Paris">Europe/Paris</option>
                <option value="Europe/Berlin">Europe/Berlin</option>
                <option value="Europe/Kyiv">Europe/Kyiv</option>
                <option value="Asia/Tokyo">Asia/Tokyo</option>
                <option value="Asia/Singapore">Asia/Singapore</option>
                <option value="Australia/Sydney">Australia/Sydney</option>
            </select>
        </div>

    {# ---- color ---------------------------------------------------- #}
    {% elif w.kind == "color" %}
        <input type="color" id="w_{{ w.name }}" name="{{ w.name }}"
               value="{% if w.value %}{{ w.value }}{% else %}#000000{% endif %}"
               {% if w.read_only %}disabled{% endif %}>

    {# ---- file ----------------------------------------------------- #}
    {% elif w.kind == "file" %}
        <input type="file" id="w_{{ w.name }}" name="{{ w.name }}"
               {% if w.required %}required{% endif %}
               {% if w.read_only %}disabled{% endif %}>

    {% elif w.kind == "mediapicker" %}
        {# Image chooser: the shared overlay (thumbnails + search, backed by the
           built-in "media" chooser) with a live preview of the selection.
           cms-ux.js wires the picker + preview via the data-chooser attrs. #}
        <div class="rcms-chooser rcms-media-chooser"
             data-chooser
             data-chooser-kind="media">
            <input type="hidden"
                   id="w_{{ w.name }}"
                   name="{{ w.name }}"
                   value="{{ w.value | default(value='') }}"
                   data-chooser-input>
            <img class="rcms-media-preview" data-chooser-preview alt=""
                 onerror="this.style.display='none'"
                 {% if w.value %}src="/__media__/raw/{{ w.value }}"{% else %}hidden{% endif %}>
            <button type="button"
                    class="rcms-btn rcms-btn-outlined"
                    data-chooser-open
                    {% if w.read_only %}disabled{% endif %}>
                <span class="material-symbols-rounded sm">image</span>
                <span data-chooser-label>
                    {%- if w.value -%}#{{ w.value }}{%- else -%}{{ "Choose an image…" | translate(locale=LANG) }}{%- endif -%}
                </span>
            </button>
            <button type="button" class="rcms-btn rcms-btn-text rcms-btn-small" data-chooser-clear
                    {% if not w.value %}style="display:none"{% endif %}
                    {% if w.read_only %}disabled{% endif %}>
                {{ "Clear" | translate(locale=LANG) }}
            </button>
        </div>

    {% elif w.kind == "pagechooser" or w.kind == "snippetchooser" or w.kind == "documentchooser" %}
        {# Chooser widgets. Hidden input carries the
           target row's id as a string (or comma-separated ids in
           multi mode); visible button opens the shared modal dialog
           (cms-ux.js wires it via the `data-chooser-kind`
           attribute). Snippet chooser can narrow to one type via
           `w.custom_name`. Multi-pick mode opts in via
           `w.variant = "multi"` (renders checkbox-per-row + a
           "Pick selected" button — a multiple-chooser
           panel). #}
        {% set chooser_kind = w.kind | replace(from="chooser", to="") %}
        {% set_global chooser_icon = "article" %}
        {% if chooser_kind == "snippet" %}{% set_global chooser_icon = "library_books" %}{% endif %}
        {% if chooser_kind == "document" %}{% set_global chooser_icon = "description" %}{% endif %}
        {% set multi = w.variant | default(value="") == "multi" %}
        {% set_global placeholder_single = "Choose a page…" | translate(locale=LANG) %}
        {% if chooser_kind == "snippet" %}{% set_global placeholder_single = "Choose a snippet…" | translate(locale=LANG) %}{% endif %}
        {% if chooser_kind == "snippet" and w.custom_name == "form" %}{% set_global placeholder_single = "Choose a form…" | translate(locale=LANG) %}{% endif %}
        {% if chooser_kind == "document" %}{% set_global placeholder_single = "Choose a document…" | translate(locale=LANG) %}{% endif %}
        {% set_global placeholder_multi = "Choose pages…" | translate(locale=LANG) %}
        {% if chooser_kind == "snippet" %}{% set_global placeholder_multi = "Choose snippets…" | translate(locale=LANG) %}{% endif %}
        {% if chooser_kind == "snippet" and w.custom_name == "form" %}{% set_global placeholder_multi = "Choose forms…" | translate(locale=LANG) %}{% endif %}
        {% if chooser_kind == "document" %}{% set_global placeholder_multi = "Choose documents…" | translate(locale=LANG) %}{% endif %}
        {% if multi %}{% set_global placeholder = placeholder_multi %}{% else %}{% set_global placeholder = placeholder_single %}{% endif %}
        <div class="rcms-chooser"
             data-chooser
             data-chooser-kind="{{ chooser_kind }}"
             {% if multi %}data-chooser-multi="1"{% endif %}
             {% if w.custom_name %}data-chooser-filter="{{ w.custom_name }}"{% endif %}
             data-chooser-placeholder="{{ placeholder }}">
            <input type="hidden"
                   id="w_{{ w.name }}"
                   name="{{ w.name }}"
                   value="{{ w.value | default(value='') }}"
                   data-chooser-input>
            <button type="button"
                    class="rcms-btn rcms-btn-outlined"
                    data-chooser-open
                    {% if w.read_only %}disabled{% endif %}>
                <span class="material-symbols-rounded sm">{{ chooser_icon }}</span>
                <span data-chooser-label>
                    {%- if w.value -%}
                        {%- if multi -%}{{ w.value | split(pat=",") | length }} {{ "selected" | translate(locale=LANG) }}{%- else -%}#{{ w.value }}{%- endif -%}
                    {%- else -%}{{ placeholder }}{%- endif -%}
                </span>
            </button>
            {% if w.value %}
                <button type="button" class="rcms-btn rcms-btn-text rcms-btn-small" data-chooser-clear
                        {% if w.read_only %}disabled{% endif %}>
                    {{ "Clear" | translate(locale=LANG) }}
                </button>
            {% endif %}
        </div>

    {# #421 — generic model chooser. `w.custom_name` is the registered
       chooser slug; it becomes `data-chooser-kind`, which cms-ux.js
       resolves to `/cms-admin/__chooser/<slug>` (an unregistered kind
       falls back to the generic endpoint). Single-pick id value. #}
    {% elif w.kind == "modelchooser" %}
        <div class="rcms-chooser"
             data-chooser
             data-chooser-kind="{{ w.custom_name }}">
            <input type="hidden"
                   id="w_{{ w.name }}"
                   name="{{ w.name }}"
                   value="{{ w.value | default(value='') }}"
                   data-chooser-input>
            <button type="button"
                    class="rcms-btn rcms-btn-outlined"
                    data-chooser-open
                    {% if w.read_only %}disabled{% endif %}>
                <span class="material-symbols-rounded sm">widgets</span>
                <span data-chooser-label>
                    {%- if w.value -%}#{{ w.value }}{%- else -%}{{ w.placeholder | default(value="Choose…" | translate(locale=LANG)) }}{%- endif -%}
                </span>
            </button>
            {% if w.value %}
                <button type="button" class="rcms-btn rcms-btn-text rcms-btn-small" data-chooser-clear
                        {% if w.read_only %}disabled{% endif %}>
                    {{ "Clear" | translate(locale=LANG) }}
                </button>
            {% endif %}
        </div>

    {# ---- choice variants ----------------------------------------- #}
    {% elif w.kind == "radio" %}
        <div class="rcms-radio-group">
            {% for opt in w.options %}
                <label class="rcms-radio-option">
                    <input type="radio" name="{{ w.name }}" value="{{ opt.0 }}"
                           {% if w.value == opt.0 %}checked{% endif %}
                           {% if w.required and loop.first %}required{% endif %}
                           {% if w.read_only %}disabled{% endif %}>
                    {{ opt.1 }}
                </label>
            {% endfor %}
        </div>

    {% elif w.kind == "select" %}
        <select id="w_{{ w.name }}" name="{{ w.name }}"
                {% if w.required %}required{% endif %}
                {% if w.read_only %}disabled{% endif %}>
            {% if not w.required %}<option value="">{{ "— none —" | translate(locale=LANG) }}</option>{% endif %}
            {% for opt in w.options %}
                <option value="{{ opt.0 }}"{% if w.value == opt.0 %} selected{% endif %}>{{ opt.1 }}</option>
            {% endfor %}
        </select>

    {% elif w.kind == "checkboxes" %}
        {# Multi-value contract: the form POSTs ONE hidden field per
           checkbox-group whose `value` is a JSON array string. The
           visible checkboxes are page-local; a tiny `change` listener
           on the form repopulates the hidden field. v1 ships the JS
           handler in the bundled stream-editor module; for v1
           extension-form-only use, host apps that don't want the
           full bundle can echo the array in a `<script>` block. #}
        <div class="rcms-checkbox-group" data-widget-multivalue="checkboxes" data-name="{{ w.name }}">
            <input type="hidden" name="{{ w.name }}" value="{{ w.value | default(value='[]') }}">
            {% for opt in w.options %}
                <label class="rcms-checkbox-option">
                    <input type="checkbox" value="{{ opt.0 }}"
                           data-multivalue-option
                           {% if w.value and (w.value is containing(opt.0)) %}checked{% endif %}
                           {% if w.read_only %}disabled{% endif %}>
                    {{ opt.1 }}
                </label>
            {% endfor %}
        </div>

    {% elif w.kind == "multiselect" %}
        {# Native `<select multiple>` posts each selected `<option>`
           value as a SEPARATE form key — same multi-value collision
           issue as `checkboxes`. We emit a hidden JSON-array field
           the JS shim keeps in sync; the visible `<select multiple>`
           is for UX only and has `name=""` to suppress its own
           submission. #}
        <div class="rcms-multiselect-group" data-widget-multivalue="multiselect" data-name="{{ w.name }}">
            <input type="hidden" name="{{ w.name }}" value="{{ w.value | default(value='[]') }}">
            <select multiple size="6" data-multivalue-source
                    {% if w.read_only %}disabled{% endif %}>
                {% for opt in w.options %}
                    <option value="{{ opt.0 }}"
                            {% if w.value and (w.value is containing(opt.0)) %}selected{% endif %}>
                        {{ opt.1 }}
                    </option>
                {% endfor %}
            </select>
        </div>

    {# #243 — Page↔Snippet many-to-many chooser. Renders structurally
       identical to `multiselect` (hidden JSON-array form value + a
       visible `<select multiple>` whose change events the JS shim
       mirrors into the hidden input). The options list is populated
       server-side by the page-type handler — it queries cms_snippet
       for `w.custom_name == type_name` and stamps `(id, title)`
       pairs into `w.options` before rendering. The shim picks this
       up via `data-widget-multivalue="multiselect"` so no new JS
       wiring is needed. Hosts call
       `rustango_cms::page_snippet_m2m::replace_all(pool, page_id,
       w.name /* relation */, parsed_ids)` from their
       `save_extension` override; `related_snippets(pool, page_id,
       relation)` from `load_extension`. #}
    {% elif w.kind == "snippetm2m" %}
        <div class="rcms-multiselect-group" data-widget-multivalue="multiselect" data-name="{{ w.name }}">
            <input type="hidden" name="{{ w.name }}" value="{{ w.value | default(value='[]') }}">
            <select multiple size="6" data-multivalue-source
                    {% if w.read_only %}disabled{% endif %}>
                {% for opt in w.options %}
                    <option value="{{ opt.0 }}"
                            {% if w.value and (w.value is containing(opt.0)) %}selected{% endif %}>
                        {{ opt.1 }}
                    </option>
                {% endfor %}
            </select>
            {% if w.custom_name %}
                <p class="rcms-hint rcms-hint--sm">
                    Snippets of type <code>{{ w.custom_name }}</code>. Hold ⌘ / Ctrl to multi-select.
                </p>
            {% endif %}
        </div>

    {# ---- streamfield --------------------------------------------- #}
    {% elif w.kind == "stream" %}
        {# The admin handler pre-renders the entire stream editor
           (existing blocks + per-allowed-type <template> clones) via
           `block::admin::render_stream_editor` and stashes the HTML
           on `w.custom_html`. The hidden input here is the single
           form field that submits the serialized JSON tree; the JS
           keeps it in sync on every `input` event. #}
        <div class="rcms-stream-editor"
             data-stream-root
             data-stream-name="{{ w.name }}"
             data-allowed="{{ w.allowed | join(sep=',') }}">
            {# Hidden input MUST sit inside `data-stream-root` so the
               JS `serializeAll()` walker (`:scope > [data-stream-input]`)
               finds it. #}
            <input type="hidden" name="{{ w.name }}" value="{{ w.value }}" data-stream-input>
            {{ w.custom_html | safe }}
            <div class="rcms-stream-picker">
                <button type="button" class="rcms-btn rcms-btn-outlined rcms-btn-small" data-action="open-picker" data-insert-at="end">
                    <span class="material-symbols-rounded sm">add</span> {{ "Add block" | translate(locale=LANG) }}
                </button>
            </div>

            {# Per-stream block picker dialog. Native <dialog> for
               built-in keyboard nav + a11y. Filtered to data-allowed
               at open time by the JS. #}
            {# The picker dialog lives inside the outer `<form id="page-edit-form">`.
               HTML5 disallows nested forms, so use plain `type="button"`
               for every action inside this dialog — submit-typed buttons
               would bubble to the outer form and navigate away. The JS
               (stream_editor.js) handles close + pick-block as click
               delegations. #}
            <dialog class="rcms-block-picker" data-block-picker>
                <header>
                    <strong>{{ "Choose a block" | translate(locale=LANG) }}</strong>
                    <button type="button" class="rcms-btn rcms-btn-outlined rcms-btn-small" data-action="close-picker" aria-label="{{ "Close" | translate(locale=LANG) }}">
                        <span class="material-symbols-rounded sm">close</span>
                    </button>
                </header>
                {# #411 — client-side filter. stream_editor.js hides
                   non-matching tiles + their now-empty group headers. #}
                <input type="search" class="rcms-block-picker-search" data-block-picker-search
                       placeholder="{{ "Search blocks…" | translate(locale=LANG) }}" aria-label="{{ "Search blocks" | translate(locale=LANG) }}" autocomplete="off">
                <ul class="rcms-block-picker-list">
                    {% for opt in picker_options | default(value=[]) %}
                        <li class="rcms-block-picker-option"
                            data-block-option
                            data-type="{{ opt.type_name }}"
                            data-label="{{ opt.verbose_name }}"
                            {% if opt.group %}data-group="{{ opt.group }}"{% endif %}
                            {% if opt.collapsed %}data-collapsed="1"{% endif %}
                            {% if opt.default_value %}data-default-value='{{ opt.default_value | json_script }}'{% endif %}>
                            <button type="button" class="rcms-block-picker-tile" data-action="pick-block" data-type="{{ opt.type_name }}"
                                    {% if opt.description %}title="{{ opt.description }}"{% endif %}>
                                {% if opt.icon %}<span class="material-symbols-rounded">{{ opt.icon }}</span>{% endif %}
                                <span class="rcms-block-picker-tile-label">
                                    <span class="rcms-block-picker-tile-name">{{ opt.verbose_name }}</span>
                                    {% if opt.description %}<span class="rcms-block-picker-tile-desc">{{ opt.description }}</span>{% endif %}
                                    {% if opt.preview_html %}
                                        <span class="rcms-block-picker-tile-preview">{{ opt.preview_html | safe }}</span>
                                    {% endif %}
                                </span>
                            </button>
                        </li>
                    {% endfor %}
                </ul>
            </dialog>
        </div>

    {# ---- custom escape hatch ------------------------------------- #}
    {% elif w.kind == "custom" %}
        {# The admin handler pre-renders each custom widget by
           looking up its template via
           `widget::registry::find_custom_widget` and stashing the
           HTML in `w.custom_html`. Empty `custom_html` means the
           name had no registration — surface visibly so the
           misconfiguration is debuggable in-editor. (Tera's
           `{% include %}` requires a string literal, hence the
           pre-render dance — same shape `cms_snippet` uses.) #}
        {% if w.custom_html %}
            {{ w.custom_html | safe }}
        {% else %}
            <div class="rcms-hint rcms-hint--error">
                ⚠ custom widget <code>{{ w.custom_name }}</code> has no registered template.
                Did you call <code>register_widget!("{{ w.custom_name }}", "templates/widgets/...")</code>?
            </div>
        {% endif %}

    {% else %}
        <div class="rcms-hint rcms-hint--warning">
            ⚠ unknown widget kind <code>{{ w.kind }}</code> — please report.
        </div>
    {% endif %}

    {% if w.help and w.kind != "boolean" %}<div class="rcms-hint">{{ w.help }}</div>{% endif %}
</div>
{%- endmacro %}