webmamba/one-file-twig-component-bundle
Composer 安装命令:
composer require webmamba/one-file-twig-component-bundle
包简介
Let you create your Twig Component with template, CSS, an, and Javascript in one file
README 文档
README
Caution
This Bundle not really aim for to be production ready and run on professional website, it's more like a POC.
This Bundle allow you to define Twig Component with his HTML, CSS, and Javascript in one file.
<div {{ attributes }}> <h3>Counter</h3> <button data-action='click->avatar#increment'>Click</button> <p>count <span data-avatar-target="count">0</span></p> </div> <style> h3 { color: yellow; } </style> <script> import { Controller } from "@hotwired/stimulus" export default class extends Controller { connect() { this.count = 0; } static targets = ['count']; increment() { this.count++; this.countTarget.textContent = this.count; } } </script>
This Bundle make also your CSS and your Javascript scoped to this component, so by using this bundle your sure that your CSS or Javascript will conflic with the rest of the page.
Install
In your Symfony application install the bundle :
composer require webmamba/one-file-twig-component-bundle
then add the bundle to your config/bundles.php:
return [ // Webmamba\OneFileTwigComponentBundle\OneFileTwigComponentBundle::class => ['all' => true], ];
you need to tweak a bit the assets/bootstrap.js:
import { startStimulusApp } from '@symfony/stimulus-bundle'; import Clipboard from 'stimulus-clipboard'; const app = startStimulusApp(); app.register('clipboard', Clipboard); const elements = document.querySelectorAll('[data-ux-component-controller-files]'); console.log(elements); elements.forEach((element) => { let file = element.getAttribute('data-ux-component-controller-files'); let id = element.getAttribute('data-ux-component-id'); import(file).then((result) => { app.register(id, result.default) ; }); });
and last step add the following config lines in your config/packages/twig.yaml
twig: // ... paths: '%kernel.project_dir%/var/component-assets': ~
Congrats 🍾 the bundle is now fully installed.
How to use it ?
You can now go on any component template you want and run the following :
<p>Hello world</p> <style> {# The CSS you need #} </style> <script> {# the JavaScript your need #} </script>
Why it's not on Symfony UX ?
I am not happy with the implementation right now. Few issue :
- We don't leverage AssetMapper
- I listen to the kernel.response event to modify the importmap
- We should have more cache
- We should have a command or something so when we deploy we compile all the CSS and Javascript from the TwigComponents template
- ...
How could you help ?
You think you have a better implemenatation, you know how to fix the issues above, or anything, do a pull request or an issue. Everything here is open to discution. Thanks! See you soon!
webmamba/one-file-twig-component-bundle 适用场景与选型建议
webmamba/one-file-twig-component-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 webmamba/one-file-twig-component-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webmamba/one-file-twig-component-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-11