Use Computed Properties for lazy loading and caching
3.xTo keep components lazy, avoid heavy logic in the constructor. Instead, use methods to fetch data.
If you call a method directly in Twig (e.g., {{ this.products }}), it will execute every time it is called. To implement true computed properties (where the result is cached for the duration of the render), use the computed proxy.
Important: Do NOT use #[ExposeInTemplate] on a computed method, as this will cause the method to be called twice (once for the attribute and once for the template), defeating the purpose of the cache.
{# templates/components/FeaturedProducts.html.twig #}
<div>
<h3>Featured Products</h3>
{% for product in computed.products %}
...
{% endfor %}
{# This uses the cached value and does not trigger a second query #}
{% for product in computed.products %}
...
{% endfor %}
</div>