survos / js-twig-bundle
wrap the jstwig library in a Symfony UX component
Package info
github.com/survos/js-twig-bundle
Language:JavaScript
Type:symfony-bundle
pkg:composer/survos/js-twig-bundle
Fund package maintenance!
Requires
- php: ^8.4
- survos/core-bundle: ^2.0
- symfony/config: ^7.4||^8.0
- symfony/dependency-injection: ^7.4||^8.0
- symfony/dom-crawler: ^7.4||^8.0
- symfony/http-kernel: ^7.4||^8.0
- symfony/ux-twig-component: ^2.8
- twig/twig: ^3.4
Requires (Dev)
- phpstan/phpstan: ^2.0
- roave/security-advisories: dev-latest
- symfony/browser-kit: ^7.4||^8.0
- symfony/framework-bundle: ^7.4||^8.0
- symfony/phpunit-bridge: ^7.4||^8.0
- symfony/twig-bundle: ^7.4||^8.0
- symfony/var-dumper: ^7.4||^8.0
- dev-main
- 2.0.124
- 2.0.123
- 2.0.122
- 2.0.121
- 2.0.120
- 2.0.119
- 2.0.117
- 2.0.116
- 2.0.115
- 2.0.114
- 2.0.113
- 2.0.112
- 2.0.111
- 2.0.110
- 2.0.109
- 2.0.108
- 2.0.107
- 2.0.106
- 2.0.105
- 2.0.104
- 2.0.103
- 2.0.102
- 2.0.101
- 2.0.100
- 2.0.99
- 2.0.98
- 2.0.97
- 2.0.96
- 2.0.95
- 2.0.94
- 2.0.93
- 2.0.92
- 2.0.91
- 2.0.90
- 2.0.89
- 2.0.88
- 2.0.87
- 2.0.86
- 2.0.85
- 2.0.84
- 2.0.83
- 2.0.82
- 2.0.81
- 2.0.80
- 2.0.79
- 2.0.78
- 2.0.77
- 2.0.76
- 2.0.75
- 2.0.74
- 2.0.73
- 2.0.72
- 2.0.71
- 2.0.70
- 2.0.69
- 2.0.68
- 2.0.67
- 2.0.66
- 2.0.65
- 2.0.64
- 2.0.63
- 2.0.62
- 2.0.61
- 2.0.60
- 2.0.59
- 2.0.58
- 2.0.57
- 2.0.56
- 2.0.55
- 2.0.54
- 2.0.53
- 2.0.51
- 2.0.50
- 2.0.49
- 2.0.48
- 2.0.47
- 2.0.46
- 2.0.45
- 2.0.44
- 2.0.43
- 2.0.42
- 2.0.41
- 2.0.40
- 2.0.39
- 2.0.38
- 2.0.37
- 2.0.36
- 2.0.35
- 2.0.34
- 2.0.33
- 2.0.32
- 2.0.31
- 2.0.30
- 2.0.29
- 2.0.28
- 2.0.27
- 2.0.26
- 2.0.25
- 2.0.24
- 2.0.23
- 2.0.22
- 2.0.21
- 2.0.20
- 2.0.19
- 2.0.18
- 2.0.17
- 2.0.16
- 2.0.15
- 2.0.14
- 2.0.13
- 2.0.12
- 2.0.11
- 1.6.44
- 1.6.43
- 1.6.42
- 1.6.41
- 1.6.40
- 1.6.39
- 1.6.38
- 1.6.37
- 1.6.36
- 1.6.35
- 1.6.34
- 1.6.33
- 1.6.32
- 1.6.31
- 1.6.30
- 1.6.29
- 1.6.28
- 1.6.27
- 1.6.26
- 1.6.25
- 1.6.24
- 1.6.23
- 1.6.22
- 1.6.21
- 1.6.20
- 1.6.19
- 1.6.18
- 1.6.17
- 1.6.16
- 1.6.15
- 1.6.14
- 1.6.13
- 1.6.12
- 1.6.11
- 1.6.10
- 1.6.9
- 1.6.8
- 1.6.7
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.529
- 1.5.528
- 1.5.527
- 1.5.526
- 1.5.525
- 1.5.524
- 1.5.523
- 1.5.522
- 1.5.521
- 1.5.520
- 1.5.519
- 1.5.518
- 1.5.517
- 1.5.516
- 1.5.515
- 1.5.514
- 1.5.513
- 1.5.512
- 1.5.511
- 1.5.510
- 1.5.509
- 1.5.508
- 1.5.507
- 1.5.506
- 1.5.505
- 1.5.504
- 1.5.503
- 1.5.502
- 1.5.501
- 1.5.500
- 1.5.499
- 1.5.498
- 1.5.497
- 1.5.496
- 1.5.495
- 1.5.494
- 1.5.493
- 1.5.492
- 1.5.491
- 1.5.490
- 1.5.489
- 1.5.488
- 1.5.487
- 1.5.486
- 1.5.485
- 1.5.484
- 1.5.483
- 1.5.482
- 1.5.481
- 1.5.480
- 1.5.479
- 1.5.478
- 1.5.477
- 1.5.476
- 1.5.475
- 1.5.474
- 1.5.473
- 1.5.472
- 1.5.471
- 1.5.470
- 1.5.469
- 1.5.468
- 1.5.467
- 1.5.466
- 1.5.465
- 1.5.464
- 1.5.463
- 1.5.462
- 1.5.461
- 1.5.460
- 1.5.459
- 1.5.458
- 1.5.457
- 1.5.456
- 1.5.455
- 1.5.454
- 1.5.453
- 1.5.452
- 1.5.451
- 1.5.450
- 1.5.449
- 1.5.448
- 1.5.447
- 1.5.446
- 1.5.445
- 1.5.444
- 1.5.443
- 1.5.442
- 1.5.441
- 1.5.440
- 1.5.439
- 1.5.438
- 1.5.437
- 1.5.436
- 1.5.435
- 1.5.434
- 1.5.433
- 1.5.432
- 1.5.431
- 1.5.430
- 1.5.429
- 1.5.428
- 1.5.427
- 1.5.426
- 1.5.425
- 1.5.424
- 1.5.423
- 1.5.422
- 1.5.421
- 1.5.420
- 1.5.419
- 1.5.418
- 1.5.417
- 1.5.416
- 1.5.415
- 1.5.414
- 1.5.413
- 1.5.412
- 1.5.411
- 1.5.410
- 1.5.409
- 1.5.408
- 1.5.407
- 1.5.406
- 1.5.405
- 1.5.404
- 1.5.403
- 1.5.402
- 1.5.401
- 1.5.400
- 1.5.399
- 1.5.398
- 1.5.397
- 1.5.396
- 1.5.395
- 1.5.394
- 1.5.393
- 1.5.392
- 1.5.391
- 1.5.390
- 1.5.389
- 1.5.388
- 1.5.387
- 1.5.386
- 1.5.385
- 1.5.384
- 1.5.383
- 1.5.382
- 1.5.381
- 1.5.380
- 1.5.379
- 1.5.378
- 1.5.377
- 1.5.376
- 1.5.375
- 1.5.374
- 1.5.373
- 1.5.372
- 1.5.371
- 1.5.370
- 1.5.369
- 1.5.368
- 1.5.367
- 1.5.366
- 1.5.365
- 1.5.364
- 1.5.363
- 1.5.362
- 1.5.361
- 1.5.360
- 1.5.359
- 1.5.358
- 1.5.357
- 1.5.356
- 1.5.355
- 1.5.354
- 1.5.353
- 1.5.352
- 1.5.351
- 1.5.350
- 1.5.345
- 1.5.344
- 1.5.343
- 1.5.342
- 1.5.341
- 1.5.340
- 1.5.339
- 1.5.338
- 1.5.337
- 1.5.336
- 1.5.335
- 1.5.334
- 1.5.333
- 1.5.332
- 1.5.331
- 1.5.330
- 1.5.329
- 1.5.328
- 1.5.327
- 1.5.326
- 1.5.325
- 1.5.324
- 1.5.323
- 1.5.322
- 1.5.321
- 1.5.320
- 1.5.319
- 1.5.318
- 1.5.317
- 1.5.316
- 1.5.315
- 1.5.314
- 1.5.313
- 1.5.312
- 1.5.311
- 1.5.310
- 1.5.309
- 1.5.308
- 1.5.307
- 1.5.306
- 1.5.305
- 1.5.304
- 1.5.303
- 1.5.302
- 1.5.301
- 1.5.300
- 1.5.299
- 1.5.298
- 1.5.297
- 1.5.296
- 1.5.295
- 1.5.294
- 1.5.293
- 1.5.292
- 1.5.291
- 1.5.290
- 1.5.289
- 1.5.288
- 1.5.287
- 1.5.286
- 1.5.285
- 1.5.284
- 1.5.283
- 1.5.282
- 1.5.281
- 1.5.280
- 1.5.279
- 1.5.278
- 1.5.277
- 1.5.276
- 1.5.275
- 1.5.274
- 1.5.273
- 1.5.272
- 1.5.271
- 1.5.270
- 1.5.269
- 1.5.268
- 1.5.267
- 1.5.266
- 1.5.265
- 1.5.264
- 1.5.263
- 1.5.262
- 1.5.261
- 1.5.260
- 1.5.259
- 1.5.258
- 1.5.257
- 1.5.256
- 1.5.255
- 1.5.254
- 1.5.253
- 1.5.252
- 1.5.251
- 1.5.250
- 1.5.249
- 1.5.248
- 1.5.247
- 1.5.246
- 1.5.245
- 1.5.244
- 1.5.243
- 1.5.242
- 1.5.241
- 1.5.240
- 1.5.239
- 1.5.238
- 1.5.237
- 1.5.236
- 1.5.235
- 1.5.234
- 1.5.233
- 1.5.232
- 1.5.231
- 1.5.230
- 1.5.229
- 1.5.228
- 1.5.227
- 1.5.226
- 1.5.225
- 1.5.224
- 1.5.223
- 1.5.222
- 1.5.221
- 1.5.220
- 1.5.219
- 1.5.218
- 1.5.217
- 1.5.216
- 1.5.215
- 1.5.214
- 1.5.213
- 1.5.212
- 1.5.211
- 1.5.210
- 1.5.209
- 1.5.208
- 1.5.207
- 1.5.206
- 1.5.205
- 1.5.204
- 1.5.203
This package is auto-updated.
Last update: 2026-03-06 17:41:47 UTC
README
Render Twig-like blocks in the browser from Stimulus controllers.
This bundle provides:
- A Symfony UX/TwigComponent bridge that extracts
<twig:block>templates from Twig files. - A browser-side twig.js API with Symfony-friendly helpers (
path,stimulus_*,ux_icon,render). - A block compiler/renderer utility for rendering a specific named block (for example a grid cell template in
api-grid-bundle). - A per-request manifest registry (block names + source hashes) for debug/discovery.
- An optional Dexie-based data/rendering pipeline.
Why this exists
Server-rendered Twig is still the source of truth for markup, but some UI pieces need to be rendered client-side after async data is loaded. The primary use case is:
- define block templates in Twig
- pass those templates to JavaScript
- render specific blocks on demand from a Stimulus controller
Current modules
assets/src/lib/twig_api.js: installs Twig functions used by client-side templates.assets/src/lib/twig_blocks.js: compiles/renders named block registries.src/TwigBlocksTrait.php: extracts<twig:block>content from a caller template.src/Components/JsTwigComponent.php+assets/src/controllers/js_twig_controller.js: legacy UX component/controller path.src/Components/DexieTwigComponent.php+assets/src/controllers/dexie_controller.js: Dexie integration path.
Recommended path for new work
For reusable rendering (for example, "render one cell block in a Stimulus grid controller"), prefer the library API:
installTwigAPI(...)compileTwigBlocks(registry, scriptTagId)twigRender(registry, blockName, data)
This is more composable than hard-coding rendering logic in a single controller.
Manifest + runtime debug
The bundle now emits two debug layers:
- Server manifest registry (per request): each component contributes a manifest id, caller template, slot names, and source hashes.
- Runtime usage tracking (browser): compiled registries and rendered block slots are captured in
window.__jstwigDebugSnapshot().
To show an on-page debug panel, enable bundle debug config and render:
{{ jstwig_debug_panel() }}
The panel shows:
- server manifest registry
- compiled registries on current page
- block slots rendered on current page
1) Emit blocks JSON from Twig
Use a script tag that contains a JSON object blockName -> template string.
{# Example: include this near the Stimulus root element #} <script type="application/json" id="api-grid-cell-blocks" data-caller="{{ _self }}"> {{ this.twigBlocks|json_encode|raw }} </script>
this.twigBlocks comes from TwigBlocksTrait in a TwigComponent.
2) Compile and render in a Stimulus controller
import { Controller } from '@hotwired/stimulus'; import * as StimAttrs from 'stimulus-attributes'; import { installTwigAPI } from '@survos/js-twig-bundle/twig_api'; import { compileTwigBlocks, twigRender } from '@survos/js-twig-bundle/twig_blocks'; export default class extends Controller { connect() { this.tpl = {}; installTwigAPI({ StimAttrs, blockRegistry: this.tpl }); compileTwigBlocks(this.tpl, 'api-grid-cell-blocks'); } renderCell(row) { return twigRender(this.tpl, 'cell', { data: row, globals: { locale: 'en' } }); } }
3) Define blocks in Twig
<twig:block name="cell"> <span class="cell-label">{{ data.label }}</span> </twig:block>
Legacy jsTwig component usage
The bundle still ships <twig:jsTwig> and js_twig_controller.js for direct fetch-and-render flows, but new integrations should prefer the reusable block registry approach above.
Twig functions available in browser templates
After installTwigAPI(...), twig.js templates can call:
path(route, params)render(blockName, vars)stimulus_controller(name, values, classes, outlets)stimulus_target(name, target)stimulus_action(name, action, event, params)ux_icon(name, attrs)sais_encode(url)
See assets/src/lib/twig_api.js for behavior and fallbacks.
Dexie integration (optional)
<twig:dexie> and dexie_controller.js support loading data into Dexie and rendering list/detail blocks. This is useful for offline/mobile-like patterns, but has a larger API surface and more coupling than the block registry flow.
If your goal is only "render Twig blocks from Stimulus", you usually do not need Dexie.
Configuration (Dexie path)
Bundle configuration keys are defined in src/SurvosJsTwigBundle.php:
dbversionstores[]withname,schema,url,batch,response_key
AI/Human docs map
README.md: entry point and quick usage.docs/ARCHITECTURE.md: internal contracts and data flow.docs/AI_AGENT_GUIDE.md: implementation conventions for contributors/agents.docs/IMPROVEMENTS.md: prioritized code improvements.docs/EVENT_DRIVEN_EXAMPLE.md: real-world event-driven rendering pattern.