@@ -158,6 +158,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
158158 $ io ->text ([
159159 'Next: ' ,
160160 \sprintf ('- Open <info>%s</info> and add the code you need ' , $ filePath ),
161+ '- Use the controller in your templates: ' ,
162+ ...array_map (
163+ fn (string $ line ): string => " $ line " ,
164+ explode ("\n" , $ this ->generateUsageExample ($ controllerName , $ targetArgs , $ valuesArg , $ classesArgs )),
165+ ),
161166 'Find the documentation at <fg=yellow>https://symfony.com/bundles/StimulusBundle</> ' ,
162167 ]);
163168 }
@@ -287,6 +292,51 @@ private function getValuesTypes(): array
287292 ];
288293 }
289294
295+ /**
296+ * @param array<int, string> $targets
297+ * @param array{name: string, type: string} $values
298+ * @param array<int, string> $classes
299+ */
300+ private function generateUsageExample (string $ name , array $ targets , array $ values , array $ classes ): string
301+ {
302+ $ slugify = fn (string $ name ) => str_replace ('_ ' , '- ' , Str::asSnakeCase ($ name ));
303+ $ controller = $ slugify ($ name );
304+
305+ $ htmlTargets = [];
306+ foreach ($ targets as $ target ) {
307+ $ htmlTargets [] = \sprintf ('<div data-%s-target="%s"></div> ' , $ controller , $ target );
308+ }
309+
310+ $ htmlValues = [];
311+ foreach ($ values as ['name ' => $ name , 'type ' => $ type ]) {
312+ $ value = match ($ type ) {
313+ 'Array ' => '[] ' ,
314+ 'Boolean ' => 'false ' ,
315+ 'Number ' => '123 ' ,
316+ 'Object ' => '{} ' ,
317+ 'String ' => 'abc ' ,
318+ default => '' ,
319+ };
320+ $ htmlValues [] = \sprintf ('data-%s-%s-value="%s" ' , $ controller , $ slugify ($ name ), $ value );
321+ }
322+
323+ $ htmlClasses = [];
324+ foreach ($ classes as $ class ) {
325+ $ value = Str::asLowerCamelCase ($ class );
326+ $ htmlClasses [] = \sprintf ('data-%s-%s-class="%s" ' , $ controller , $ slugify ($ class ), $ value );
327+ }
328+
329+ return \sprintf (
330+ '<div data-controller="%s"%s%s%s>%s%s</div> ' ,
331+ $ controller ,
332+ $ htmlValues ? ("\n " .implode ("\n " , $ htmlValues )) : '' ,
333+ $ htmlClasses ? ("\n " .implode ("\n " , $ htmlClasses )) : '' ,
334+ $ htmlValues || $ htmlClasses ? "\n" : '' ,
335+ $ htmlTargets ? ("\n " .implode ("\n " , $ htmlTargets )) : '' ,
336+ "\n <!-- ... --> \n" ,
337+ );
338+ }
339+
290340 public function configureDependencies (DependencyBuilder $ dependencies ): void
291341 {
292342 // lower than 8.1, allow WebpackEncoreBundle
0 commit comments