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