Skip to content

Conversation

alexislefebvre
Copy link
Contributor

Q A
Bug fix? no
New feature? no
Deprecations? no
Documentation? yes
Issues -
License MIT

Here is a “trick” to define default values for anonymous components.

@carsonbot carsonbot added Documentation Improvements or additions to documentation Status: Needs Review Needs to be reviewed labels Oct 9, 2025
@carsonbot carsonbot changed the title TwigComponent: explain how to define default values TwigComponent: explain how to define default values Oct 9, 2025
@alexislefebvre alexislefebvre changed the title TwigComponent: explain how to define default values [TwigComponent] explain how to define default values Oct 9, 2025
Comment on lines 721 to 743
Default values
~~~~~~~~~~~~~~

The ``{% props %}`` tag can also be used to define default values of an anonymous component.

.. code-block:: html+twig

{# templates/components/Image.html.twig #}
{% props size = 24 %}

<img src="/image.jpg" width="{{ size }}" height="{{ size }}" alt="">

Then you can call the component with or without defining this value:

.. code-block:: html+twig

<twig:Image size="48"/>
<twig:Image/>

{# renders as: #}
<img src="/image.jpg" width="48" height="48" alt="">
<img src="/image.jpg" width="24" height="24" alt="">

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a new section for default properties values, and the code examples seem wrong to me. What about changing the previous code block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think is wrong in these examples?

The previous code blocks rely on {{ attributes.defaults({class: 'primary'}) }}: https://symfony.com/bundles/ux-twig-component/current/index.html#anonymous-components I preferred to separate these 2 different usages of props.

Comment on lines 707 to 717
.. code-block:: html+twig

{# templates/components/Button.html.twig #}
{% props icon = null, type = 'primary' %}

<button {{ attributes.defaults({class: 'btn btn-'~type}) }}>
{% block content %}{% endblock %}
{% if icon %}
<span class="fa-solid fa-{{ icon }}"></span>
{% endif %}
</button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, what about making icon prop required:

Suggested change
.. code-block:: html+twig
{# templates/components/Button.html.twig #}
{% props icon, type = 'primary' %}
<button {{ attributes.defaults({class: 'btn btn-'~type}) }}>
{% block content %}{% endblock %}
{% if icon %}
<span class="fa-solid fa-{{ icon }}"></span>
{% endif %}
</button>

and add some examples:

{# property "icon" is missing, an exception is thrown #}
<twig:Button>Share</twig:Button>

{# property "icon" is passed, property "type" use its default value "primary" #}
<twig:Button icon="share">Share</twig:Button> 

{# both properties "icon" and "type" are passed #}
<twig:Button icon="share" type="secondary></twig:Button>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I added it below the current examples because I think that showing props icon = null + {% if icon %} is interesting too.

@carsonbot carsonbot added Status: Needs Work Additional work is needed and removed Status: Needs Review Needs to be reviewed labels Oct 10, 2025
@carsonbot carsonbot changed the title [TwigComponent] explain how to define default values explain how to define default values Oct 10, 2025
@carsonbot carsonbot added Status: Needs Review Needs to be reviewed and removed Status: Needs Work Additional work is needed labels Oct 10, 2025
@alexislefebvre alexislefebvre changed the title explain how to define default values [doc] explain how to define default values Oct 10, 2025
@alexislefebvre alexislefebvre changed the title [doc] explain how to define default values [TwigComponent] explain how to define default values Oct 10, 2025
@alexislefebvre alexislefebvre changed the title [TwigComponent] explain how to define default values [TwigComponent] explain how to define mandatory and default values Oct 10, 2025
@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Oct 17, 2025
@carsonbot carsonbot changed the title [TwigComponent] explain how to define mandatory and default values explain how to define mandatory and default values Oct 17, 2025
@carsonbot carsonbot changed the title explain how to define mandatory and default values [TwigComponent] explain how to define mandatory and default values Oct 17, 2025
@Kocal Kocal changed the title [TwigComponent] explain how to define mandatory and default values Document how to define mandatory and default values Oct 17, 2025
@Kocal Kocal changed the title Document how to define mandatory and default values Document how to define mandatory props and their default values Oct 17, 2025
@Kocal Kocal changed the title Document how to define mandatory props and their default values Document how to define mandatory/default props Oct 17, 2025
@Kocal
Copy link
Member

Kocal commented Oct 17, 2025

Thank you @alexislefebvre.

@Kocal Kocal merged commit ddf0222 into symfony:2.x Oct 17, 2025
2 checks passed
@alexislefebvre alexislefebvre deleted the patch-4 branch October 17, 2025 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Status: Reviewed Has been reviewed by a maintainer TwigComponent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants