Symfony UX Central Piece

Stimulus Bundle

Connects Stimulus, UX packages, AssetMapper, Symfony Reprise... Making it easy to add JavaScript interactivity to your Symfony apps!

composer require symfony/stimulus-bundle

What Stimulus Bundle gives you

  • Controllers, connected

    Wire behaviour to the markup you already write.

  • Twig helpers

    stimulus_controller, stimulus_target and stimulus_action.

  • Asset pipeline

    Works with AssetMapper and with Symfony Reprise.

  • Lazy loading

    Load a controller when its element appears.

  • Web standards

    Plain DOM events and data attributes, nothing more.

Stimulus Twig helpers

Wire a controller from your template.

Three Twig functions write the data attributes Stimulus looks for. The markup stays readable, and the controller stays plain JavaScript.

stimulus_controller

This function attaches a Stimulus controller to an HTML element and allows passing values (via data-attributes) that can be accessed within the controller.

These values are useful for providing dynamic data or configuration to your controller’s logic.

<div {{ stimulus_controller('userProfile', {userId: 42, theme: 'dark'}) }}>
    Welcome to your profile!
</div>

{# would render as #}

<div data-controller="userProfile" data-user-profile-user-id-value="42"
     data-user-profile-theme-value="dark">
    Welcome to your profile!
</div>

stimulus_target

This function defines one or more targets within a Stimulus controller. These targets allow you to interact with specific DOM elements directly from your controller’s logic.

<div {{ stimulus_controller('userProfile') }}>
    <span {{ stimulus_target('userProfile', 'name') }}>John Doe</span>
    <span {{ stimulus_target('userProfile', 'avatar') }}>
        <img src="avatar.jpg" alt="John's Avatar">
    </span>
</div>

{# would render as #}

<div data-controller="userProfile">
    <span data-user-profile-target="name">John Doe</span>
    <span data-user-profile-target="avatar">
        <img src="avatar.jpg" alt="John's Avatar">
    </span>
</div>

stimulus_action

This function attaches an event listener to an HTML element, defining actions that trigger specific methods in the controller.

This simplifies event handling by mapping DOM events (like click, input, etc.) directly to controller methods, improving the clarity and maintainability of your code.

<button {{ stimulus_action('userProfile', 'save', 'click') }}>
    Save Profile
</button>

{# would render as #}

<button data-action="click->userProfile#save">
    Save Profile
</button>

What the bundle wires for you

Controllers you never have to register.

Beyond the Twig helpers, the bundle owns the plumbing: where controllers come from, when they load, and how new ones are created.

Third-party controllers

Every UX package declares its controllers in controllers.json. The bundle reads that file and loads them.

{
    "controllers": {
        "@symfony/ux-autocomplete": {
            "autocomplete": {
                "enabled": true,
                "fetch": "lazy"
            }
        }
    }
}

Loaded when needed

One comment keeps a controller out of the initial payload until an element asks for it.

/* stimulusFetch: 'lazy' */
export default class extends Controller {
    connect() {
        // ...
    }
}

Scaffolded by Maker

A command writes the controller in the right place, already registered.

php bin/console make:stimulus-controller

Install Stimulus.

$ composer require symfony/stimulus-bundle

More from Symfony UX.

Stimulus is one package among many. They share the same Stimulus foundation and install the same way.

Image for the Inspector UX package

Inspector

Browser devtools for Stimulus controllers, Live Components and Turbo frames.

Image for the Toolkit UX package

Toolkit

Collection of components and templates that you can use to build your pages.

Image for the Native UX package

Native

Build native mobile apps that wrap your Symfony web application

Browse every package

Keep Stimulus at hand.

Documentation, the people behind it, and where to ask when something does not behave.