Skip to content

Commit cddafa8

Browse files
committed
minor #2456 [TwigComponent] Document about unwanted behavior with ExposeInTemplate and computed methods (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- [TwigComponent] Document about unwanted behavior with `ExposeInTemplate` and computed methods | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> As spotted when working on my application, the following code triggers two times the methods `transactions()` because of the presence of `#[ExposeInTemplate]` (because before I used `transactions` instead of `computed.transactions`): ```php #[ExposeInTemplate] public function transactions(): array { // SQL query to database } ``` ```twig {{ computed.transactions|length }} ``` When I remove `#[ExposeInTemplate]`, then only one SQL query is made. Commits ------- 16cd48a [TwigComponent] Document about unwanted behavior with ExposeInTemplate and computed methods
2 parents c3ee75b + 16cd48a commit cddafa8

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/TwigComponent/doc/index.rst

+7-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ use the full path of the template where the macro is defined:
382382
<twig:Alert>
383383
{# ❌ this won't work #}
384384
{% from _self import message_formatter %}
385-
385+
386386
{# ✅ this works as expected #}
387387
{% from 'path/of/this/template.html.twig' import message_formatter %}
388388

@@ -1553,6 +1553,12 @@ are called additional times, the cached value is used.
15531553
Computed methods only work for component methods with no required
15541554
arguments.
15551555

1556+
.. tip::
1557+
1558+
Ensure to not use the ``ExposeInTemplate`` attribute on a computed method,
1559+
otherwise the method will be called twice instead of only once, leading to
1560+
unnecessary overhead and potential performance issues.
1561+
15561562
Events
15571563
------
15581564

0 commit comments

Comments
 (0)