@@ -415,6 +415,135 @@ you can store its result on a private property:
415415 }
416416 }
417417
418+ Component Attributes
419+ --------------------
420+
421+ A common need for components is to configure/render attributes for the
422+ root node. You can enable this feature by having your component use
423+ the ``HasAttributesTrait ``. Attributes are any data passed to ``component() ``
424+ that cannot be mounted on the component itself. This extra data is added
425+ to a ``ComponentAttributes `` object that lives as a public property on your
426+ component (available as ``attributes `` in your component's template).
427+
428+ To use, add the HasAttributesTrait to your component:
429+
430+ use Symfony\U X\T wigComponent\A ttribute\A sTwigComponent;
431+ use Symfony\U X\T wigComponent\H asAttributesTrait;
432+
433+ #[AsTwigComponent('my_component')]
434+ class MyComponent
435+ {
436+ use HasAttributesTrait;
437+ }
438+
439+ .. code-block :: twig
440+
441+ {# templates/components/my_component.html.twig #}
442+
443+ <div{{ attributes }}>
444+ My Component!
445+ </div>
446+
447+ When rendering the component, you can pass an array of html attributes to add:
448+
449+ .. code-block :: twig
450+
451+ {{ component('my_component', { class: 'foo', style: 'color:red' }) }}
452+
453+ {# renders as: #}
454+ <div class="foo" style="color:red">
455+ My Component!
456+ </div>
457+
458+ Defaults
459+ ~~~~~~~~
460+
461+ Set default attributes that can be fully overridden by passed attributes:
462+
463+ .. code-block :: twig
464+
465+ {# templates/components/my_component.html.twig #}
466+
467+ <div{{ attributes.defaults({ class: 'bar' }) }}>
468+ My Component!
469+ </div>
470+
471+ {# render component #}
472+ {{ component('my_component', { style: 'color:red' }) }}
473+ {{ component('my_component', { class: 'foo', style: 'color:red' }) }}
474+
475+ {# renders as: #}
476+ <div class="bar" style="color:red">
477+ My Component!
478+ </div>
479+ <div class="foo" style="color:red">
480+ My Component!
481+ </div>
482+
483+ Merging Defaults
484+ ~~~~~~~~~~~~~~~~
485+
486+ Set defaults but allow them to be appended to by passing these values to
487+ the ``component() `` function:
488+
489+ .. code-block :: twig
490+
491+ {# templates/components/my_component.html.twig #}
492+
493+ <div{{ attributes.merge({ class: 'bar' }) }}>
494+ My Component!
495+ </div>
496+
497+ {# render component #}
498+ {{ component('my_component', { class: 'foo', style: 'color:red' }) }}
499+
500+ {# renders as: #}
501+ <div class="bar foo" style="color:red">
502+ My Component!
503+ </div>
504+
505+ Only
506+ ~~~~
507+
508+ Extract specific attributes and discard the rest:
509+
510+ .. code-block :: twig
511+
512+ {# templates/components/my_component.html.twig #}
513+
514+ <div{{ attributes.only('class') }}>
515+ My Component!
516+ </div>
517+
518+ {# render component #}
519+ {{ component('my_component', { class: 'foo', style: 'color:red' }) }}
520+
521+ {# renders as: #}
522+ <div class="foo">
523+ My Component!
524+ </div>
525+
526+ Without
527+ ~~~~~~~
528+
529+ Exclude specific attributes:
530+
531+ .. code-block :: twig
532+
533+ {# templates/components/my_component.html.twig #}
534+
535+ <div{{ attributes.without('class') }}>
536+ My Component!
537+ </div>
538+
539+ {# render component #}
540+ {{ component('my_component', { class: 'foo', style: 'color:red' }) }}
541+
542+ {# renders as: #}
543+ <div style="color:red">
544+ My Component!
545+ </div>
546+
418547 Embedded Components
419548-------------------
420549
0 commit comments