[TwigComponent] Document about unwanted behavior with ExposeInTemplate and computed methods#2456
Conversation
|
Nothing illogical to me, but a mention in documentation won't hurt and clarify this case. 👍 Could you maybe use a "tip" block instead of a "warning" one here ? |
| .. warning:: | ||
|
|
||
| Ensure to not use the ``ExposeInTemplate`` attribute on a computed method, | ||
| otherwise the method will be called twice instead of only once, leading to |
There was a problem hiding this comment.
It kinda leads to present it as a bug... but it is not illogical.
The calls are not made at the same time so the "twice" is maybe a bit missleading
(first time to build the context of the templates, before render, and one by you calling the cache object).
There was a problem hiding this comment.
Do you think instead, we should fix this behavior?
I mean, throw an exception if the user tries to call a computed method with #[ExposeInTemplate] ?
There was a problem hiding this comment.
I am not sure we can. BUT we can maybe populate the computed before the render if method as the attribute. There are some limitations but this could work.
Lets update doc for now and see after if we change and how wdyt ?
(but id rather not use warning or any tone that can stress or frighten users 😇)
…e and computed methods
647f5f5 to
16cd48a
Compare
smnandre
left a comment
There was a problem hiding this comment.
We may want to make all these more clear with schema later..
But this precision is usefull right now, thx!
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 usedtransactionsinstead ofcomputed.transactions):#[ExposeInTemplate] public function transactions(): array { // SQL query to database }{{ computed.transactions|length }}When I remove
#[ExposeInTemplate], then only one SQL query is made.