Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TwigComponent] Document about unwanted behavior with ExposeInTemplate and computed methods #2456

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ use the full path of the template where the macro is defined:
<twig:Alert>
{# ❌ this won't work #}
{% from _self import message_formatter %}

{# ✅ this works as expected #}
{% from 'path/of/this/template.html.twig' import message_formatter %}

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

.. tip::

Ensure to not use the ``ExposeInTemplate`` attribute on a computed method,
otherwise the method will be called twice instead of only once, leading to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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] ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😇)

unnecessary overhead and potential performance issues.

Events
------

Expand Down
Loading