Skip to content

Commit df4c94b

Browse files
TwigComponent: explain how to define default values
1 parent 38a248f commit df4c94b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,31 @@ To tell the system that ``icon`` and ``type`` are props and not attributes, use
716716
{% endif %}
717717
</button>
718718

719+
You can specify mandatory and optional props:
720+
721+
.. code-block:: html+twig
722+
723+
{# templates/components/Button.html.twig #}
724+
{% props icon, type = 'primary' %}
725+
726+
<button {{ attributes.defaults({class: 'btn btn-'~type}) }}>
727+
{% block content %}{% endblock %}
728+
<span class="fa-solid fa-{{ icon }}"></span>
729+
</button>
730+
731+
Examples of usage:
732+
733+
.. code-block:: html+twig
734+
735+
{# property "icon" is missing, an exception is thrown #}
736+
<twig:Button>Share</twig:Button>
737+
738+
{# property "icon" is passed, property "type" use its default value "primary" #}
739+
<twig:Button icon="share">Share</twig:Button>
740+
741+
{# both properties "icon" and "type" are passed #}
742+
<twig:Button icon="share" type="secondary>Share</twig:Button>
743+
719744
.. _embedded-components:
720745

721746
Passing HTML to Components Via Blocks

0 commit comments

Comments
 (0)