承接 survos/js-twig-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

survos/js-twig-bundle

Composer 安装命令:

composer require survos/js-twig-bundle

包简介

wrap the jstwig library in a Symfony UX component

关键字:

README 文档

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 { createEngine } from '@tacman1123/twig-browser';
import { installSymfonyTwigAPI } from '@tacman1123/twig-browser/adapters/symfony';
import { compileTwigBlocks } from '@tacman1123/twig-browser/src/compat/compileTwigBlocks.js';
import { path } from '@survos/js-twig/generated/fos_routes.js';

export default class extends Controller {
  connect() {
    this.tpl = {};
    this.engine = createEngine();
    installSymfonyTwigAPI(this.engine, { pathGenerator: path });
    compileTwigBlocks(this.engine, this.tpl, 'api-grid-cell-blocks');
  }

  renderCell(row) {
    return this.engine.renderBlock('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 installSymfonyTwigAPI(...), 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)

Prefer @tacman1123/twig-browser directly for all new code. assets/src/lib/twig_api.js remains only as a legacy shim.

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:

  • db
  • version
  • stores[] with name, 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.

survos/js-twig-bundle 适用场景与选型建议

survos/js-twig-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.83k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony-ux」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 survos/js-twig-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 survos/js-twig-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 survos/js-twig-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-17