Skip to content

Commit 221e22e

Browse files
committed
minor #1549 [LiveComponent] Add doc about the placeholder macro (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Add doc about the placeholder macro Add documentation about #1532 (placeholder macro) Commits ------- a7c2cb3 [LiveComponent] Add doc about the placeholder macro
2 parents a5bb3d1 + a7c2cb3 commit 221e22e

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,8 +2242,12 @@ Deferring / Lazy Loading Components
22422242
If a component is heavy to render, you can defer rendering it until after
22432243
the page has loaded. To do this, add the ``defer`` option:
22442244

2245-
.. code-block:: twig
2245+
.. code-block:: html+twig
22462246

2247+
{# With the HTML syntax #}
2248+
<twig:SomeHeavyComponent defer />
2249+
2250+
{# With the component function #}
22472251
{{ component('SomeHeavyComponent', { defer: true }) }}
22482252

22492253
This renders an empty ``<div>`` tag, but triggers an Ajax call to render the
@@ -2255,21 +2259,82 @@ real component once the page has loaded.
22552259
page load, but it isn't rendered. So keep your heavy work to methods in
22562260
your component (e.g. ``getProducts()``) that are only called when rendering.
22572261

2258-
To add some loading text before the real component is loaded, use the
2259-
``loading-template`` option to point to a template:
2262+
You can define some content to be rendered while the component is loading, either
2263+
inside the component template (the ``placeholder`` macro) or from the calling template
2264+
(the ``loading-template`` attribute and the ``loadingContent`` block).
22602265

2261-
.. code-block:: twig
2266+
.. versionadded:: 2.17.0
2267+
2268+
Defining a placeholder macro into the component template was added in Live Components 2.17.0.
2269+
2270+
In the component template, define a ``placeholder`` macro, outside of the
2271+
component's main content. This macro will be called when the component is deferred:
2272+
2273+
.. code-block:: html+twig
22622274

2275+
{# templates/recommended-products.html.twig #}
2276+
<div {{ attributes }}>
2277+
{# This will be rendered when the component is fully loaded #}
2278+
{% for product in this.products %}
2279+
<div>{{ product.name }}</div>
2280+
{% endfor %}
2281+
</div>
2282+
2283+
{% macro placeholder(props) %}
2284+
{# This content will (only) be rendered as loading content #}
2285+
<span class="loading-row"></span>
2286+
{% endmacro %}
2287+
2288+
The ``props`` argument contains the props passed to the component.
2289+
You can use it to customize the placeholder content. Let's say your
2290+
component shows a certain number of products (defined with the ``size``
2291+
prop). You can use it to define a placeholder that shows the same
2292+
number of rows:
2293+
2294+
.. code-block:: html+twig
2295+
2296+
{# In the calling template #}
2297+
<twig:RecommendedProducts size="3" defer />
2298+
2299+
.. code-block:: html+twig
2300+
2301+
{# In the component template #}
2302+
{% macro placeholder(props) %}
2303+
{% for i in 1..props.size %}
2304+
<div class="loading-product">
2305+
...
2306+
</div>
2307+
{% endfor %}
2308+
{% endmacro %}
2309+
2310+
To customize the loading content from the calling template, you can use
2311+
the ``loading-template`` option to point to a template:
2312+
2313+
.. code-block:: html+twig
2314+
2315+
{# With the HTML syntax #}
2316+
<twig:SomeHeavyComponent defer loading-template="spinning-wheel.html.twig" />
2317+
2318+
{# With the component function #}
22632319
{{ component('SomeHeavyComponent', { defer: true, loading-template: 'spinning-wheel.html.twig' }) }}
22642320

22652321
Or override the ``loadingContent`` block:
22662322

2267-
.. code-block:: twig
2323+
.. code-block:: html+twig
2324+
2325+
{# With the HTML syntax #}
2326+
<twig:SomeHeavyComponent defer>
2327+
<twig:block name="loadingContent">Custom Loading Content...</twig:block>
2328+
</twig:SomeHeavyComponent>
22682329

2330+
{# With the component tag #}
22692331
{% component SomeHeavyComponent with { defer: true } %}
22702332
{% block loadingContent %}Loading...{% endblock %}
22712333
{% endcomponent %}
22722334

2335+
When ``loading-template`` or ``loadingContent`` is defined, the ``placeholder``
2336+
macro is ignored.
2337+
22732338
To change the initial tag from a ``div`` to something else, use the ``loading-tag`` option:
22742339

22752340
.. code-block:: twig

0 commit comments

Comments
 (0)