rahid/macros-assets
最新稳定版本:1.0.0
Composer 安装命令:
composer require rahid/macros-assets
包简介
Un bundle Symfony pour gérer les macros Twig liées aux assets.
README 文档
README
A Symfony bundle for registering and rendering Twig macro assets in your templates.
Table of Contents
Description
This bundle allows to track Twig macros usage at runtime across your Symfony application, so CSS and JS can be loaded dynamically depending on the macros used in the rendered template.
Installation
Requirements
- Symfony 6.0 or higher
- PHP 8.1 or higher
Step 1: Add the project repository
In the main composer.json, add:
"repositories": [ { "type": "vcs", "url": "https://github.com/rhidja/macros-assets.git" } ]
Step 2: Install via Composer
composer require rhidja/macros-assets
Step 3: Register the bundle
Add the bundle to your config/bundles.php file:
return [ // ... Rahid\MacrosAssets\RahidMacrosAssets::class => ['all' => true], ];
Step 4: Configure macros
Create config/packages/macros_assets.yaml:
rahid_macros_assets: input: css: - build/css/style-1.css - https://style-2.css js: - build/js/script-1.js - build/js/script-2.js textarea: css: - build/css/style-3.css - https://style-4.css js: - build/js/script-5.js - build/js/script-3.js
Usage
Define your macros with logging
{# templates/macros/form_macros.twig #} {% macro input(name, value = '', type = 'text') %} {{ _log_macro('input') }} <input type="{{ type }}" name="{{ name }}" value="{{ value }}"> {% endmacro %} {% macro textarea(name, value = '') %} {{ _log_macro('textarea') }} <textarea name="{{ name }}" id="{{ name }}" cols="30" rows="10">{{ value }}</textarea> {% endmacro %}
Use macros in your template
{% import 'macros/form_macros.twig' as forms %}
<form method="post">
{{ forms.input('username') }}
{{ forms.textarea('description') }}
<button type="submit">Submit</button>
</form>
Render assets after all macros are called
Place this at the end of your layout (after all macro calls):
{{ _render_assets() }}
This will dynamically insert the CSS and JS associated with the macros used in the template.
Contributing
Contributions are welcome! To propose improvements or report issues, please open an issue or submit a pull request.
License
This project is licensed under the MIT License – see the LICENSE file for details.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-19