rahid/macros-assets

Un bundle Symfony pour gérer les macros Twig liées aux assets.

Maintainers

Package info

github.com/rhidja/macros-assets

Type:symfony-bundle

pkg:composer/rahid/macros-assets

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2025-10-08 10:15 UTC

This package is auto-updated.

Last update: 2026-04-19 16:20:36 UTC


README

GitHub release (latest SemVer) GitHub issues GitHub license

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.