{"id":29828063,"url":"https://github.com/hellosebastian/hello-stimulus-bundle","last_synced_at":"2025-07-29T07:16:10.032Z","repository":{"id":49896789,"uuid":"358346958","full_name":"HelloSebastian/hello-stimulus-bundle","owner":"HelloSebastian","description":"Symfony Bundle with Stimulus.js Helper functions","archived":false,"fork":false,"pushed_at":"2021-06-08T17:20:25.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-30T04:04:36.683Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HelloSebastian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-15T17:53:26.000Z","updated_at":"2021-06-08T17:20:20.000Z","dependencies_parsed_at":"2022-08-29T17:52:10.984Z","dependency_job_id":null,"html_url":"https://github.com/HelloSebastian/hello-stimulus-bundle","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/HelloSebastian/hello-stimulus-bundle","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HelloSebastian%2Fhello-stimulus-bundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HelloSebastian%2Fhello-stimulus-bundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HelloSebastian%2Fhello-stimulus-bundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HelloSebastian%2Fhello-stimulus-bundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HelloSebastian","download_url":"https://codeload.github.com/HelloSebastian/hello-stimulus-bundle/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HelloSebastian%2Fhello-stimulus-bundle/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267645236,"owners_count":24120877,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-29T07:16:09.009Z","updated_at":"2025-07-29T07:16:10.020Z","avatar_url":"https://github.com/HelloSebastian.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HelloStimulusBundle\n\n**This Bundle provides Twig and Symfony form helper functions for [Stimulus.js](https://stimulus.hotwire.dev/).** \n\n## Overview\n\n1. [Features](#features)\n2. [Installation](#installation)\n3. [Twig helper functions](#twig-helper-functions)\n4. [Form helper functions](#form-helper-functions)\n\n## Features\n\n* Twig helper functions\n* Symfony Forms helper functions\n\n## Installation\n\n### Step 1: Download the Bundle\n\nOpen a command console, enter your project directory and execute the following command to download this bundle:\n\n``` bash\n$ composer require hello-sebastian/hello-stimulus-bundle\n```\n\n### Step 2: Enable the Bundle (without flex)\n\nThen, enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:\n\n``` php\n// config/bundles.php\n\nreturn [\n    // ...\n    HelloSebastian\\HelloStimulusBundle\\HelloStimulusBundle::class =\u003e ['all' =\u003e true],\n];\n```\n\n\n\n## Twig helper functions\n\n### hello_stimulus_controller(controllerName, values = [])\n\nRender value controller attribute. Optional with values.\n\n#### Parameters\n\n**controllerName**: name of the controller\n\n**values**: array of stimulus values (optional)\n\n#### Examples\n\n```twig\n\u003cdiv {{ hello_stimulus_controller('hello') }}\u003e\u003c/div\u003e\n\n\u003cdiv {{ hello_stimulus_controller('hello', [\n\t{name: 'myValue', value: 'Hey!'},\n\t{name: 'myNumber', value: 1234}\n]) }}\u003e\u003c/div\u003e\n```\n\nis rendered to\n\n```html\n\u003cdiv data-controller=\"hello\"\u003e\u003c/div\u003e\n\n\u003cdiv data-controller=\"hello\" data-hello-my-value-value=\"Hey!\" data-hello-my-number-value=\"1234\"\u003e\u003c/\u003cdiv\u003e\n```\n\nYou can use two ways to specify the controller:\n\n*Assuming the controller is located at `assets/controllers/user/user_form_controller.js`*\n\n- the \"HTML\" stimulus type\n\n  ```twig\n  \u003cdiv {{ hello_stimulus_controller('user--user-form') }}\u003e\u003c/div\u003e\n  {# rendered to data-controller=\"user--user-form\" #}\n  ```\n\n- the \"JavaScript\" type\n\n  ```twig\n  \u003cdiv {{ hello_stimulus_controller('user/user_form') }}\u003e\u003c/div\u003e\n  {# rendered to data-controller=\"user--user-form\" #}\n  ```\n\nBoth variants give the same result.\n\n\n\n### hello_stimulus_target(controllerName, target)\n\nRender value target attribute.\n\n#### Parameters\n\n**controllerName**: name of controller for this target\n\n**target**: name of the target attribute\n\n#### Examples\n\n```twig\n\u003ch1 {{ hello_stimulus_target('hello', 'greeting') }}\u003e\u003c/h1\u003e\n```\n\nis rendered to\n\n```html\n\u003cdiv data-hello-target=\"greeting\"\u003e\u003c/div\u003e\n```\n\n\n\n### hello_stimulus_action(controllerName, event, method)\n\nRender action data attribute.\n\n#### Parameters\n\n**controllerName**: name of controller for this action\n\n**event**: DOM event to listen for\n\n**method**: name of the JavaScript method inside the controller class\n\n#### Examples\n\n```twig\n\u003cbutton type=\"button\" {{ hello_stimulus_action('hello', 'click', 'handleBtnClick') }}\u003e\n    Hey!\n\u003c/button\u003e\n```\n\nis rendered to\n\n```html\n\u003cbutton type=\"button\" data-action=\"click-\u003ehello#handleBtnClick\"\u003e\n    Hey!\n\u003c/button\u003e\n```\n\n\n\n### hello_stimulus_value(controllerName, name, value)\n\nRender value data attribute.\n\n#### Parameters\n\n**controllerName**: name of controller for this value\n\n**name**: name of this value\n\n**value**: value of this value\n\n#### Examples\n\n```twig\n\u003cdiv {{ hello_stimulus_value('hello', 'username', 'hello_sebastian') }}\u003e\u003c/div\u003e\n```\n\nis rendered to\n\n```html\n\u003cdiv data-hello-username-value=\"hello_sebastian\"\u003e\u003c/div\u003e\n```\n\n\n\n## Form helper functions\n\nIn Symfony Forms it is helpful to pass attributes of stimulus directly to the types. For this purpose, this bundle provides a helper class with two methods (`target()` and `value()`).\n\n[Full example of StimulusFormHelper](#example)\n\n### StimulusFormHelper API\n\n#### __construct(controllerName, defaultEvent = \"click\")\n\n##### Paramters\n\n**controllerName**: name (and location) of the JavaScript controller class\n\n**defaultEvent**: (optional): default DOM event to listen for (default is the \"click\" event)\n\n##### Usage\n\nYou can use two ways to specify the controller:\n\n*Assuming the controller is located at `assets/controllers/user/user_form_controller.js`*\n\n- the \"HTML\" stimulus type\n\n  ```php\n  $this-\u003eformController = new StimulusFormHelper('user--user-form');\n  // rendered to data-controller=\"user--user-form\" to the form tag\n  ```\n\n- the \"JavaScript\" type\n\n  ```php\n  $this-\u003eformController = new StimulusFormHelper('user/user_form');\n  // rendered to data-controller=\"user--user-form\" to the form tag\n  ```\n\nBoth variants give the same result.\n\n\n\n#### target(target)\n\n##### Parameters\n\n**target**: name of target property inside stimulus controller\n\n##### Returns\n\n```php\narray(\"data-[controllerName]-target\" =\u003e \"[target]\");\n```\n\n##### Usage\n\n```php\n-\u003eadd('firstName', TextType::class, array(\n    'label' =\u003e 'First name',\n    'attr' =\u003e $this-\u003eformController-\u003etarget('firstNameInput')\n))\n```\n\n\n\n#### action(method, event = null)\n\n##### Parameters\n\n**method**: name of JavaScript method inside stimulus controller\n\n**event**: (optional) DOM event to listen for (if null, default event from constructor is taken)\n\n##### Returns\n\n```php\narray(\"data-action\" =\u003e \"[event]-\u003e[controllerName]#[method]\");\n```\n\n##### Usage\n\n```php\n-\u003eadd('isActive', CheckboxType::class, array(\n    'label' =\u003e 'is Active',\n    'required' =\u003e false,\n    'attr' =\u003e $this-\u003eformController-\u003eaction(\"handleIsActive\", \"change\")\n))\n```\n\n\n\n### Example\n\n```php\n//src/Form\n\nnamespace App\\Form;\n\nuse App\\Entity\\User;\nuse HelloSebastian\\HelloStimulusBundle\\Form\\StimulusFormHelper; // \u003c-- don't forget this use statement\nuse Symfony\\Bridge\\Doctrine\\Form\\Type\\EntityType;\nuse Symfony\\Component\\Form\\AbstractType;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\CheckboxType;\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType;\nuse Symfony\\Component\\Form\\FormBuilderInterface;\nuse Symfony\\Component\\Form\\FormInterface;\nuse Symfony\\Component\\Form\\FormView;\nuse Symfony\\Component\\OptionsResolver\\OptionsResolver;\n\nclass UserType extends AbstractType\n{\n    private $userFormController;\n\n    public function __construct()\n    {\n        $this-\u003eformController = new StimulusFormHelper('user-form');\n    }\n\n    public function buildForm(FormBuilderInterface $builder, array $options)\n    {\n        $builder\n            -\u003eadd('firstName', TextType::class, array(\n                'label' =\u003e 'First name',\n              \t'attr' =\u003e $this-\u003eformController-\u003etarget('firstNameInput')\n            ))\n            -\u003eadd('lastName', TextareaType::class, array(\n                'label' =\u003e 'Last name',\n              \t'label_attr' =\u003e $this-\u003eformController-\u003etarget('lastNameLabel')\n              \t'attr' =\u003e $this-\u003eformController-\u003etarget('lastNameInput')\n            ))\n            -\u003eadd('email', TextareaType::class, array(\n                'label' =\u003e 'Email'\n            ))\n          \t-\u003eadd('isActive', CheckboxType::class, array(\n                'label' =\u003e 'is Active',\n                'required' =\u003e false,\n              \n                // if you want to use both inside the same attr, you can use array_merge()\n                'attr' =\u003e array_merge(\n                    $this-\u003eformController-\u003eaction(\"handleIsActive\", \"change\"),\n                    $this-\u003eformController-\u003etarget(\"isActiveCheckbox\"),\n                  \tarray('class' =\u003e 'my-checkbox-class')\n                )\n            ))\n        ;\n    }\n  \n    public function buildView(FormView $view, FormInterface $form, array $options)\n    {\n        $this-\u003eformController-\u003esetControllerNameToFormView($view);\n    }      \n\n    public function configureOptions(OptionsResolver $resolver)\n    {\n        $resolver-\u003esetDefaults(array(\n            'data_class' =\u003e User::class,\n        ));\n    }\n}\n```\n\n`action` and `traget` return an array. If you want to use both inside the same `attr`, you can use `array_merge()`.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellosebastian%2Fhello-stimulus-bundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhellosebastian%2Fhello-stimulus-bundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellosebastian%2Fhello-stimulus-bundle/lists"}