StimulusBundle: Symfony integration with Stimulus ================================================= .. tip:: Check out live demos of Symfony UX at https://ux.symfony.com! This bundle adds integration between Symfony, `Stimulus`_ and the Symfony UX packages: * Twig ``stimulus_`` functions & filters to add Stimulus controllers, actions & targets in your templates; * Integration to load :ref:`UX Packages ` (extra Stimulus controllers) Installation ------------ First, if you don't have one yet, choose and install an asset handling system; both work great with StimulusBundle: * `AssetMapper`_: PHP-based system for handling assets or * `Webpack Encore`_ Node-based packaging system See `Encore vs AssetMapper`_ to learn which is best for your project. Next, install the bundle: .. code-block:: terminal $ composer require symfony/stimulus-bundle If you're using `Symfony Flex`_, you're done! The recipe will update the necessary files. If not, or you're curious, see :ref:`Manual Setup `. .. tip:: If you're using Encore, be sure to install your assets (e.g. ``npm install``) and restart Encore. Usage ----- You can now create custom Stimulus controllers inside of the ``assets/controllers`` directory. In fact, you should have an example controller there already: ``hello_controller.js``: .. code-block:: javascript import { Controller } from '@hotwired/stimulus'; export default class extends Controller { connect() { this.element.textContent = 'Hello Stimulus! Edit me in assets/controllers/hello_controller.js'; } } Then, activate the controller in your HTML: .. code-block:: html+twig
...
Optionally, this bundle has a Twig function to render the attribute: .. code-block:: html+twig
...
...
That's it! Whenever this element appears on the page, the ``hello`` controller will activate. There's a *lot* more to learn about Stimulus. See the `Stimulus Documentation`_ for all the goodies. TypeScript Controllers ~~~~~~~~~~~~~~~~~~~~~~ If you want to use `TypeScript`_ to define your controllers, you can! Install and set up the `sensiolabs/typescript-bundle`_. Then be sure to add the ``assets/controllers`` path to the ``sensiolabs_typescript.source_dir`` configuration. Finally, create your controller in that directory and you're good to go. .. _ux-packages: The UX Packages ~~~~~~~~~~~~~~~ Symfony provides a set of UX packages that add extra Stimulus controllers to solve common problems. StimulusBundle activates any 3rd party Stimulus controllers that are mentioned in your ``assets/controllers.json`` file. This file is updated whenever you install a UX package. The official UX packages are: * `ux-autocomplete`_: Transform ``EntityType``, ``ChoiceType`` or *any* ``