-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Helper to render "data-testid" attribute, conditionally #4417
Comments
Twig callables (i.e. filters, functions and tests) already support customizing the node class used to compile them, allowing to provide custom compilation of the callable call. See the implementation of
having different return types for your function depending on where it is called looks weird to me (and probably impossible to document in a reliable way). You should rather have 2 different functions. |
Side note: if you care about micro-optimizing the function calls at runtime to avoid calling the function in prod to get an empty string as output, you should first optimize your implementation by defining a callable that does not require triggering initialization of extensions (loading the available filters, functions, etc...) at runtime in order to get the actual callable in the compiled template Optimizable callables:
Non-optimizable callables:
Second side note: your code snippet marks |
I'm closing here as I don't think this should be part of Twig core (I'm not even convinced this is a good idea to be honest). |
Hey :)
Some days ago I've suggested a new feature to the UX team, but we think it would be better if this was part of Twig or
twig/extra-test
, something like that.When you write E2E tests, and do assertions on HTML elements, texts, etc... it's a common thing to use dedicated attributes like
data-test
ordata-testid
, see :On several Symfony projects I've worked on, I implemented a dumb Twig function like
testid()
which conditionally (usually depending of kernel environment/debug mode) returnsdata-testid="..."
or not (I've simplified an existing Twig extension, IDK if this one works):And use it like that:
It's far from perfect, each time you call
testid()
, you have to check if the attribute must be rendered or not.Ofc, this can be improved with two extension/runtime instances that will either:
and depending of your environment, you register on of the instance.
But, we still have useless functions calls in production.
Ideally, we'd like this function call to be removed from the compiled Twig code. This would have no impact on production performance.
Nice to have, it would be great if
testid()
can works with array, it might make it easier to use with Twig components:WDYT? Thanks :)
The text was updated successfully, but these errors were encountered: