Skip to content

Add docs for the new Twig Component syntax #792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,86 @@ The ``with`` data is what's mounted on the component object.
.. note::

Embedded components *cannot* currently be used with LiveComponents.

Component HTML Syntax
---------------------

.. versionadded:: 2.8

This syntax was been introduced in 2.8 and is still experimental: it may change in the future.

Twig Components come with an HTML-like syntax to ease the readability of your template:

.. code-block:: html+twig

<twig:Alert></:Alert>
// or use a self-closing tag
<twig:Alert/>

You can pass props to your component by using HTML attributes. Suppose you have the following component:

.. code-block:: html+twig

// "withActions" property will be set to true
<twig:Alert withActions message="hello"></:Alert>
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong closing?


You can add the ':' prefix to your attribute to indicate that the value
should be compiled by Twig

.. code-block:: html+twig

<twig:Alert message="hello" :user="user.id"/>

// equal to
<twig:Alert message="hello" user="{{ user.id }}"/>

// and pass object, or table, or anything you imagine
<twig:Alert :foo="['col' => ['foo', 'oof']]"/>

You can pass content directly inside your component.

.. code-block:: html+twig

<twig:Alert>
// any content you want
<div>
...
</div>
</twig:Alert>

Then in your component template, This becomes a block called content:

.. code-block:: html+twig

<div class="content">
{% block content %}
// and the content will appear in here
{% endblock %}
{% block footer %}
...
{% block footer %}
Copy link
Contributor

Choose a reason for hiding this comment

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

should be endblock?

</div>

In addition to the default block, you can also add named blocks:

.. code-block:: html+twig

<twig:Alert message="hello" :user="user.id">
<twig:block name="footer">
...
</twig:block>
</twig:Alert>

And in your component template you can access your embedded block

.. code-block:: html+twig

<div class="content">
{% block footer %}
...
{% block footer %}
Copy link
Contributor

Choose a reason for hiding this comment

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

should be endblock?

</div>


Contributing
------------
Expand Down