@@ -2242,8 +2242,12 @@ Deferring / Lazy Loading Components
2242
2242
If a component is heavy to render, you can defer rendering it until after
2243
2243
the page has loaded. To do this, add the ``defer `` option:
2244
2244
2245
- .. code-block :: twig
2245
+ .. code-block :: html+ twig
2246
2246
2247
+ {# With the HTML syntax #}
2248
+ <twig:SomeHeavyComponent defer />
2249
+
2250
+ {# With the component function #}
2247
2251
{{ component('SomeHeavyComponent', { defer: true }) }}
2248
2252
2249
2253
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.
2255
2259
page load, but it isn't rendered. So keep your heavy work to methods in
2256
2260
your component (e.g. ``getProducts() ``) that are only called when rendering.
2257
2261
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).
2260
2265
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
2262
2274
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 #}
2263
2319
{{ component('SomeHeavyComponent', { defer: true, loading-template: 'spinning-wheel.html.twig' }) }}
2264
2320
2265
2321
Or override the ``loadingContent `` block:
2266
2322
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>
2268
2329
2330
+ {# With the component tag #}
2269
2331
{% component SomeHeavyComponent with { defer: true } %}
2270
2332
{% block loadingContent %}Loading...{% endblock %}
2271
2333
{% endcomponent %}
2272
2334
2335
+ When ``loading-template `` or ``loadingContent `` is defined, the ``placeholder ``
2336
+ macro is ignored.
2337
+
2273
2338
To change the initial tag from a ``div `` to something else, use the ``loading-tag `` option:
2274
2339
2275
2340
.. code-block :: twig
0 commit comments