@@ -39,9 +39,9 @@ Done! Now render it wherever you want:
39
39
Enjoy your new component!
40
40
41
41
.. image :: images/alert-example.png
42
- :alt: Example of the Alert Component
42
+ :alt: Example of the Alert Component
43
43
44
- Example of the Alert Component
44
+ Example of the Alert Component
45
45
46
46
This brings the familiar "component" system from client-side frameworks
47
47
into Symfony. Combine this with `Live Components `_, to create
@@ -136,11 +136,6 @@ Take a moment to fist pump - then come back!
136
136
Naming Your Component
137
137
~~~~~~~~~~~~~~~~~~~~~
138
138
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
-
144
139
To give your component a name, TwigComponent looks at the namespace(s)
145
140
configured in :ref: `twig_component.yaml <default_config >` and finds the
146
141
first match. If you have the recommended ``App\Twig\Components\ ``, then:
@@ -474,7 +469,7 @@ called ``mount()``::
474
469
public string $message;
475
470
public string $type = 'success';
476
471
477
- public function mount(bool $isSuccess = true)
472
+ public function mount(bool $isSuccess = true): void
478
473
{
479
474
$this->type = $isSuccess ? 'success' : 'danger';
480
475
}
@@ -561,10 +556,6 @@ The data returned from ``preMount()`` will be used as the props for mounting.
561
556
PostMount Hook
562
557
~~~~~~~~~~~~~~
563
558
564
- .. versionadded :: 2.1
565
-
566
- The ``PostMount `` hook was added in TwigComponents 2.1.
567
-
568
559
After a component is instantiated and its data mounted, you can run extra
569
560
code via the ``PostMount `` hook::
570
561
@@ -752,11 +743,11 @@ There is also a non-HTML syntax that can be used:
752
743
Context / Variables Inside of Blocks
753
744
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
754
745
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,
756
747
independent template, which extends the component's template. This has a few interesting
757
748
consequences.
758
749
759
- First, inside of ``<twig:{ Component} > ``, the ``this `` variable represents
750
+ First, inside of ``<twig:Component> ``, the ``this `` variable represents
760
751
the component you're *now * rendering *and * you have access to all of *that *
761
752
component's variables:
762
753
@@ -814,10 +805,12 @@ When overriding the ``alert_message`` block, you have access to the ``message``
814
805
815
806
.. versionadded :: 2.13
816
807
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.
818
810
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:
821
814
822
815
.. code-block :: twig
823
816
@@ -854,10 +847,6 @@ Remember though that the ``outerScope`` reference only starts once you're INSIDE
854
847
Inheritance & Forwarding "Outer Blocks"
855
848
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
856
849
857
- .. versionadded :: 2.10
858
-
859
- The ``outerBlocks `` variable was added in 2.10.
860
-
861
850
The content inside a ``<twig: `` component tag should be viewed as living in
862
851
its own, independent template, which *extends * the component's template. This means that
863
852
any 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:
972
961
973
962
<div {{ attributes.defaults(stimulus_controller('my-controller', {someValue: 'foo'})) }}>
974
963
975
- .. versionadded :: 2.9
964
+ .. note ::
965
+
966
+ The ``stimulus_controller() `` function requires ``symfony/stimulus-bundle ``.
967
+
968
+ .. code-block :: terminal
976
969
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
981
971
982
972
.. note ::
983
973
@@ -1186,21 +1176,16 @@ The nesting is recursive so you could potentially do something like this:
1186
1176
Component with Complex Variants (CVA)
1187
1177
-------------------------------------
1188
1178
1189
- .. versionadded :: 2.16
1190
-
1191
- The ``cva `` function was added in TwigComponents 2.16.
1192
-
1193
1179
.. deprecated :: 2.20
1194
1180
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 `_.
1197
1184
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:
1204
1189
1205
1190
.. code-block :: html+twig
1206
1191
@@ -1231,52 +1216,33 @@ Then use the ``color`` and ``size`` variants to select the classes needed:
1231
1216
1232
1217
.. code-block :: html+twig
1233
1218
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
-
1240
1219
<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
+ ...
1247
1221
</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
+
1250
1229
CVA and Tailwind CSS
1251
1230
~~~~~~~~~~~~~~~~~~~~
1252
1231
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:
1261
1235
1262
1236
.. code-block :: html+twig
1263
1237
1264
- {# templates/components/Alert.html.twig #}
1265
- {% props color = 'blue', size = 'md' %}
1266
-
1267
- {% set alert = cva({
1268
- // ...
1269
- }) %}
1270
-
1271
1238
<div class="{{ alert.apply({color, size}, attributes.render('class'))|tailwind_merge }}">
1272
- {% block content %}{% endblock %}
1239
+ {% block content %}{% endblock %}
1273
1240
</div>
1274
1241
1275
1242
Compound Variants
1276
1243
~~~~~~~~~~~~~~~~~
1277
1244
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:
1280
1246
1281
1247
.. code-block :: html+twig
1282
1248
@@ -1286,21 +1252,12 @@ when multiple other variant conditions are met.
1286
1252
{% set alert = cva({
1287
1253
base: 'alert',
1288
1254
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' }
1299
1257
},
1300
1258
compoundVariants: [{
1301
- // if color = red AND size = (md or lg), add the `font-bold ` class
1302
1259
color: ['red'],
1303
- size: ['md', ' lg'],
1260
+ size: ['lg'],
1304
1261
class: 'font-bold'
1305
1262
}]
1306
1263
}) %}
@@ -1312,19 +1269,14 @@ when multiple other variant conditions are met.
1312
1269
{# index.html.twig #}
1313
1270
1314
1271
<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
+ ...
1326
1273
</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>
1328
1280
1329
1281
Default Variants
1330
1282
~~~~~~~~~~~~~~~~
@@ -1334,42 +1286,33 @@ If no variants match, you can define a default set of classes to apply:
1334
1286
.. code-block :: html+twig
1335
1287
1336
1288
{# templates/components/Alert.html.twig #}
1337
- {% props color = 'blue', size = 'md' %}
1338
-
1339
1289
{% set alert = cva({
1340
1290
base: 'alert',
1341
1291
variants: {
1342
1292
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'
1351
1294
},
1352
1295
rounded: {
1353
1296
sm: 'rounded-sm',
1354
- md: 'rounded-md',
1355
- lg: 'rounded-lg',
1297
+ md: 'rounded-md'
1356
1298
}
1357
1299
},
1358
1300
defaultVariants: {
1359
- rounded: 'md',
1301
+ rounded: 'md'
1360
1302
}
1361
1303
}) %}
1362
1304
1363
- <div class="{{ alert.apply({color, size}) }}">
1364
- {% block content %}{% endblock %}
1365
- </div>
1366
-
1367
1305
{# index.html.twig #}
1368
1306
1369
- <twig:Alert color="red" size="lg" >
1370
- <div>My content</div>
1307
+ <twig:Alert color="red">
1308
+ ...
1371
1309
</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>
1373
1316
1374
1317
Test Helpers
1375
1318
------------
@@ -1600,21 +1543,13 @@ the twig template and twig variables before components are rendered::
1600
1543
PostRenderEvent
1601
1544
~~~~~~~~~~~~~~~
1602
1545
1603
- .. versionadded :: 2.5
1604
-
1605
- The ``PostRenderEvent `` was added in TwigComponents 2.5.
1606
-
1607
1546
The ``PostRenderEvent `` is called after a component has finished
1608
1547
rendering and contains the ``MountedComponent `` that was just
1609
1548
rendered.
1610
1549
1611
1550
PreCreateForRenderEvent
1612
1551
~~~~~~~~~~~~~~~~~~~~~~~
1613
1552
1614
- .. versionadded :: 2.5
1615
-
1616
- The ``PreCreateForRenderEvent `` was added in TwigComponents 2.5.
1617
-
1618
1553
Subscribing to the ``PreCreateForRenderEvent `` gives the ability to be
1619
1554
notified before a component object is created or hydrated, at the
1620
1555
very 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
1708
1643
be used.
1709
1644
1710
1645
3rd-Party Bundle
1711
- ~~~~~~~~~~~~~~~~
1646
+ ----------------
1712
1647
1713
1648
The flexibility of Twig Components is extended even further when integrated
1714
1649
with third-party bundles, allowing developers to seamlessly include pre-built
1715
1650
components into their projects.
1716
1651
1717
1652
Anonymous Components
1718
- --------------------
1653
+ ~~~~~~~~~~~~~~~~~~~~
1719
1654
1720
1655
.. versionadded :: 2.20
1721
1656
@@ -1727,11 +1662,11 @@ can reference its components directly within your Twig templates:
1727
1662
1728
1663
.. code-block :: html+twig
1729
1664
1730
- <twig:Shadcn : Button type="primary">
1665
+ <twig:Acme : Button type="primary">
1731
1666
Click me
1732
- </twig:Shadcn : Button>
1667
+ </twig:Acme : Button>
1733
1668
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
1735
1670
by a colon, and then the component path Button.
1736
1671
1737
1672
.. note ::
@@ -1740,8 +1675,8 @@ by a colon, and then the component path Button.
1740
1675
``bin/console debug:twig `` command.
1741
1676
1742
1677
The 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.
1745
1680
1746
1681
Debugging Components
1747
1682
--------------------
@@ -1807,7 +1742,6 @@ https://symfony.com/doc/current/contributing/code/bc.html
1807
1742
.. _`Stimulus controller` : https://symfony.com/bundles/StimulusBundle/current/index.html
1808
1743
.. _`CVA (Class Variant Authority)` : https://cva.style/docs/getting-started/variants
1809
1744
.. _`html_cva` : https://twig.symfony.com/doc/3.x/functions/html_cva.html
1810
- .. _`shadcn/ui` : https://ui.shadcn.com
1811
1745
.. _`tales-from-a-dev/twig-tailwind-extra` : https://github.com/tales-from-a-dev/twig-tailwind-extra
1812
1746
.. _`ignore not defined options` : https://symfony.com/doc/current/components/options_resolver.html#ignore-not-defined-options
1813
1747
.. _`Symfony MakerBundle` : https://symfony.com/bundles/SymfonyMakerBundle/current/index.html
0 commit comments