@@ -39,9 +39,9 @@ Done! Now render it wherever you want:
3939Enjoy your new component!
4040
4141.. image :: images/alert-example.png
42- :alt: Example of the Alert Component
42+ :alt: Example of the Alert Component
4343
44- Example of the Alert Component
44+ Example of the Alert Component
4545
4646This brings the familiar "component" system from client-side frameworks
4747into Symfony. Combine this with `Live Components `_, to create
@@ -136,11 +136,6 @@ Take a moment to fist pump - then come back!
136136Naming Your Component
137137~~~~~~~~~~~~~~~~~~~~~
138138
139- .. versionadded :: 2.8
140-
141- Before 2.8, passing a name to ``AsTwigComponent `` was required. Now, the
142- name is optional and defaults to the class name.
143-
144139To give your component a name, TwigComponent looks at the namespace(s)
145140configured in :ref: `twig_component.yaml <default_config >` and finds the
146141first match. If you have the recommended ``App\Twig\Components\ ``, then:
@@ -474,7 +469,7 @@ called ``mount()``::
474469 public string $message;
475470 public string $type = 'success';
476471
477- public function mount(bool $isSuccess = true)
472+ public function mount(bool $isSuccess = true): void
478473 {
479474 $this->type = $isSuccess ? 'success' : 'danger';
480475 }
@@ -561,10 +556,6 @@ The data returned from ``preMount()`` will be used as the props for mounting.
561556PostMount Hook
562557~~~~~~~~~~~~~~
563558
564- .. versionadded :: 2.1
565-
566- The ``PostMount `` hook was added in TwigComponents 2.1.
567-
568559After a component is instantiated and its data mounted, you can run extra
569560code via the ``PostMount `` hook::
570561
@@ -752,11 +743,11 @@ There is also a non-HTML syntax that can be used:
752743Context / Variables Inside of Blocks
753744~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
754745
755- The content inside of the ``<twig:{ Component} > `` should be viewed as living in its own,
746+ The content inside of the ``<twig:Component> `` should be viewed as living in its own,
756747independent template, which extends the component's template. This has a few interesting
757748consequences.
758749
759- First, inside of ``<twig:{ Component} > ``, the ``this `` variable represents
750+ First, inside of ``<twig:Component> ``, the ``this `` variable represents
760751the component you're *now * rendering *and * you have access to all of *that *
761752component's variables:
762753
@@ -814,10 +805,12 @@ When overriding the ``alert_message`` block, you have access to the ``message``
814805
815806.. versionadded :: 2.13
816807
817- The ability to refer to the scope of higher components via the ``outerScope `` variable was added in 2.13.
808+ The ability to refer to the scope of higher components via the ``outerScope ``
809+ variable was added in 2.13.
818810
819- As mentioned before, variables from lower components are merged with those from upper components. When you need
820- access to some properties or functions from higher components, that can be done via the ``outerScope... `` variable:
811+ As mentioned before, variables from lower components are merged with those from
812+ upper components. When you need access to some properties or functions from higher
813+ components, that can be done via the ``outerScope... `` variable:
821814
822815.. code-block :: twig
823816
@@ -854,10 +847,6 @@ Remember though that the ``outerScope`` reference only starts once you're INSIDE
854847 Inheritance & Forwarding "Outer Blocks"
855848~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
856849
857- .. versionadded :: 2.10
858-
859- The ``outerBlocks `` variable was added in 2.10.
860-
861850The content inside a ``<twig: `` component tag should be viewed as living in
862851its own, independent template, which *extends * the component's template. This means that
863852any blocks that live in the "outer" template are not available. However, you
@@ -972,12 +961,13 @@ To add a custom `Stimulus controller`_ to your root component element:
972961
973962 <div {{ attributes.defaults(stimulus_controller('my-controller', {someValue: 'foo'})) }}>
974963
975- .. versionadded :: 2.9
964+ .. note ::
965+
966+ The ``stimulus_controller() `` function requires ``symfony/stimulus-bundle ``.
967+
968+ .. code-block :: terminal
976969
977- The ability to use ``stimulus_controller() `` with ``attributes.defaults() ``
978- was added in TwigComponents 2.9 and requires ``symfony/stimulus-bundle ``.
979- Previously, ``stimulus_controller() `` was passed to an ``attributes.add() ``
980- method.
970+ $ composer require symfony/stimulus-bundle
981971
982972 .. note ::
983973
@@ -1186,21 +1176,16 @@ The nesting is recursive so you could potentially do something like this:
11861176Component with Complex Variants (CVA)
11871177-------------------------------------
11881178
1189- .. versionadded :: 2.16
1190-
1191- The ``cva `` function was added in TwigComponents 2.16.
1192-
11931179.. deprecated :: 2.20
11941180
1195- The ``cva `` function was deprecated in TwigComponents 2.20, and will be removed in 3.0.
1196- The function is now provided by the ``twig/html-extra:^3.12 `` package under the name `html_cva `_.
1181+ The ``cva `` function was deprecated in TwigComponents 2.20, and will be
1182+ removed in 3.0. The function is now provided by the ``twig/html-extra:^3.12 ``
1183+ package under the name `html_cva `_.
11971184
1198- `CVA (Class Variant Authority) `_ is a concept from the JavaScript world and used
1199- by the well-known `shadcn/ui `_.
1200- CVA allows you to display a component with different variants (color, size, etc.),
1201- to create highly reusable and customizable components. This is powered by a ``cva() `` Twig
1202- function where you define ``base `` classes that should always be present and then different
1203- ``variants `` and the corresponding classes:
1185+ `CVA (Class Variant Authority) `_ originates from the JavaScript ecosystem. It
1186+ enables reusable, customizable components by managing variants (e.g., color, size).
1187+ The ``cva() `` Twig function defines ``base `` classes (always applied) and variant-specific
1188+ classes:
12041189
12051190.. code-block :: html+twig
12061191
@@ -1231,52 +1216,33 @@ Then use the ``color`` and ``size`` variants to select the classes needed:
12311216
12321217.. code-block :: html+twig
12331218
1234- {# index.html.twig #}
1235- <twig:Alert color="red" size="lg">
1236- <div>My content</div>
1237- </twig:Alert>
1238- // class="alert bg-red text-lg"
1239-
12401219 <twig:Alert color="green" size="sm">
1241- <div>My content</div>
1242- </twig:Alert>
1243- // class="alert bg-green text-sm"
1244-
1245- <twig:Alert color="red" class="flex items-center justify-center">
1246- <div>My content</div>
1220+ ...
12471221 </twig:Alert>
1248- // class="alert bg-red text-md flex items-center justify-center"
1249-
1222+
1223+ {# will render as: #}
1224+
1225+ <div class="alert bg-green text-sm">
1226+ ...
1227+ </div>
1228+
12501229CVA and Tailwind CSS
12511230~~~~~~~~~~~~~~~~~~~~
12521231
1253- CVA work perfectly with Tailwind CSS. The only drawback is that you can have class conflicts.
1254- To "merge" conflicting classes together and keep only the ones you need, use the
1255- ``tailwind_merge() `` method from `tales-from-a-dev/twig-tailwind-extra `_
1256- with the ``cva() `` function:
1257-
1258- .. code-block :: terminal
1259-
1260- $ composer require tales-from-a-dev/twig-tailwind-extra
1232+ CVA integrates seamlessly with Tailwind CSS, though class conflicts may occur.
1233+ Use the ``tailwind_merge() `` function from `tales-from-a-dev/twig-tailwind-extra `_
1234+ to resolve conflicts:
12611235
12621236.. code-block :: html+twig
12631237
1264- {# templates/components/Alert.html.twig #}
1265- {% props color = 'blue', size = 'md' %}
1266-
1267- {% set alert = cva({
1268- // ...
1269- }) %}
1270-
12711238 <div class="{{ alert.apply({color, size}, attributes.render('class'))|tailwind_merge }}">
1272- {% block content %}{% endblock %}
1239+ {% block content %}{% endblock %}
12731240 </div>
12741241
12751242Compound Variants
12761243~~~~~~~~~~~~~~~~~
12771244
1278- You can define compound variants. A compound variant is a variant that applies
1279- when multiple other variant conditions are met.
1245+ Define compound variants for conditions involving multiple variants:
12801246
12811247.. code-block :: html+twig
12821248
@@ -1286,21 +1252,12 @@ when multiple other variant conditions are met.
12861252 {% set alert = cva({
12871253 base: 'alert',
12881254 variants: {
1289- color: {
1290- blue: 'bg-blue',
1291- red: 'bg-red',
1292- green: 'bg-green',
1293- },
1294- size: {
1295- sm: 'text-sm',
1296- md: 'text-md',
1297- lg: 'text-lg',
1298- }
1255+ color: { red: 'bg-red' },
1256+ size: { lg: 'text-lg' }
12991257 },
13001258 compoundVariants: [{
1301- // if color = red AND size = (md or lg), add the `font-bold ` class
13021259 color: ['red'],
1303- size: ['md', ' lg'],
1260+ size: ['lg'],
13041261 class: 'font-bold'
13051262 }]
13061263 }) %}
@@ -1312,19 +1269,14 @@ when multiple other variant conditions are met.
13121269 {# index.html.twig #}
13131270
13141271 <twig:Alert color="red" size="lg">
1315- <div>My content</div>
1316- </twig:Alert>
1317- // class="alert bg-red text-lg font-bold"
1318-
1319- <twig:Alert color="green" size="sm">
1320- <div>My content</div>
1321- </twig:Alert>
1322- // class="alert bg-green text-sm"
1323-
1324- <twig:Alert color="red" size="md">
1325- <div>My content</div>
1272+ ...
13261273 </twig:Alert>
1327- // class="alert bg-green text-lg font-bold"
1274+
1275+ {# will render as: #}
1276+
1277+ <div class="alert bg-red text-lg font-bold">
1278+ ...
1279+ </div>
13281280
13291281Default Variants
13301282~~~~~~~~~~~~~~~~
@@ -1334,42 +1286,33 @@ If no variants match, you can define a default set of classes to apply:
13341286.. code-block :: html+twig
13351287
13361288 {# templates/components/Alert.html.twig #}
1337- {% props color = 'blue', size = 'md' %}
1338-
13391289 {% set alert = cva({
13401290 base: 'alert',
13411291 variants: {
13421292 color: {
1343- blue: 'bg-blue',
1344- red: 'bg-red',
1345- green: 'bg-green',
1346- },
1347- size: {
1348- sm: 'text-sm',
1349- md: 'text-md',
1350- lg: 'text-lg',
1293+ red: 'bg-red'
13511294 },
13521295 rounded: {
13531296 sm: 'rounded-sm',
1354- md: 'rounded-md',
1355- lg: 'rounded-lg',
1297+ md: 'rounded-md'
13561298 }
13571299 },
13581300 defaultVariants: {
1359- rounded: 'md',
1301+ rounded: 'md'
13601302 }
13611303 }) %}
13621304
1363- <div class="{{ alert.apply({color, size}) }}">
1364- {% block content %}{% endblock %}
1365- </div>
1366-
13671305 {# index.html.twig #}
13681306
1369- <twig:Alert color="red" size="lg" >
1370- <div>My content</div>
1307+ <twig:Alert color="red">
1308+ ...
13711309 </twig:Alert>
1372- // class="alert bg-red text-lg font-bold rounded-md"
1310+
1311+ {# will render as: #}
1312+
1313+ <div class="alert bg-red rounded-md">
1314+ ...
1315+ </div>
13731316
13741317Test Helpers
13751318------------
@@ -1600,21 +1543,13 @@ the twig template and twig variables before components are rendered::
16001543PostRenderEvent
16011544~~~~~~~~~~~~~~~
16021545
1603- .. versionadded :: 2.5
1604-
1605- The ``PostRenderEvent `` was added in TwigComponents 2.5.
1606-
16071546The ``PostRenderEvent `` is called after a component has finished
16081547rendering and contains the ``MountedComponent `` that was just
16091548rendered.
16101549
16111550PreCreateForRenderEvent
16121551~~~~~~~~~~~~~~~~~~~~~~~
16131552
1614- .. versionadded :: 2.5
1615-
1616- The ``PreCreateForRenderEvent `` was added in TwigComponents 2.5.
1617-
16181553Subscribing to the ``PreCreateForRenderEvent `` gives the ability to be
16191554notified before a component object is created or hydrated, at the
16201555very start of the rendering process. You have access to the component
@@ -1708,14 +1643,14 @@ If a component class matches multiple namespaces, the first matched will
17081643be used.
17091644
171016453rd-Party Bundle
1711- ~~~~~~~~~~~~~~~~
1646+ ----------------
17121647
17131648The flexibility of Twig Components is extended even further when integrated
17141649with third-party bundles, allowing developers to seamlessly include pre-built
17151650components into their projects.
17161651
17171652Anonymous Components
1718- --------------------
1653+ ~~~~~~~~~~~~~~~~~~~~
17191654
17201655.. versionadded :: 2.20
17211656
@@ -1727,11 +1662,11 @@ can reference its components directly within your Twig templates:
17271662
17281663.. code-block :: html+twig
17291664
1730- <twig:Shadcn : Button type="primary">
1665+ <twig:Acme : Button type="primary">
17311666 Click me
1732- </twig:Shadcn : Button>
1667+ </twig:Acme : Button>
17331668
1734- Here, the component name is composed of the bundle's Twig namespace ``Shadcn ``, followed
1669+ Here, the component name is composed of the bundle's Twig namespace ``Acme ``, followed
17351670by a colon, and then the component path Button.
17361671
17371672.. note ::
@@ -1740,8 +1675,8 @@ by a colon, and then the component path Button.
17401675 ``bin/console debug:twig `` command.
17411676
17421677The component must be located in the bundle's ``templates/components/ `` directory. For
1743- example, the component referenced as ``<twig:Shadcn :Button> `` should have its template
1744- file at ``templates/components/Button.html.twig `` within the Shadcn bundle.
1678+ example, the component referenced as ``<twig:Acme :Button> `` should have its template
1679+ file at ``templates/components/Button.html.twig `` within the Acme bundle.
17451680
17461681Debugging Components
17471682--------------------
@@ -1807,7 +1742,6 @@ https://symfony.com/doc/current/contributing/code/bc.html
18071742.. _`Stimulus controller` : https://symfony.com/bundles/StimulusBundle/current/index.html
18081743.. _`CVA (Class Variant Authority)` : https://cva.style/docs/getting-started/variants
18091744.. _`html_cva` : https://twig.symfony.com/doc/3.x/functions/html_cva.html
1810- .. _`shadcn/ui` : https://ui.shadcn.com
18111745.. _`tales-from-a-dev/twig-tailwind-extra` : https://github.com/tales-from-a-dev/twig-tailwind-extra
18121746.. _`ignore not defined options` : https://symfony.com/doc/current/components/options_resolver.html#ignore-not-defined-options
18131747.. _`Symfony MakerBundle` : https://symfony.com/bundles/SymfonyMakerBundle/current/index.html
0 commit comments