-
-
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
[4.0] Support string literals #4123
Conversation
This allows to write a string literal using backticks. When using a literal, you don't need to escape backslashes. This is ideal for situation where you want to reference a fully qualified class name. For example in a `constant` function. For example, before you had to write: ```twig {{ constant("App\\Entity\\Class::Constant") }} ``` Now you can write: ```twig {{ constant(`App\Entity\Class::Constant`) }} ``` Besides easier to read, there is another huge benefit: when doing a text search for a fully qualified class name, it will show up.
To clarify the terminology and avoid any misunderstandings in this conversation: Twig already supports string literals, as What you suggest here is about introducing another syntax of string literals: with ` as delimiter (and still In the Javascript world, the backtick is used in template literals, offering two things harder to do without:
These two points do not offer significant advantages for Twig, as it already provides mechanisms for these needs. Therefore, I am not entirely convinced that the advantages (facilitating PHP execution in very specific cases) outweigh the costs (significant changes to the Lexer, increased maintenance, and additional documentation). But maybe there are other features/benefits i don't see here? PhpStorm already does offer support for the |
Regarding the PHPStorm screenshot: is that native or via that Symfony plugin? |
Via the Symfony plugin it seems (found this PR that seems very related to the current PR : Haehnchen/idea-php-symfony2-plugin@b358ff0 ) |
There was a discussion in the past about adding support for unescaped slashes in existing string literals (keeping the option to escape them as well), by producing a deprecation warning for any useless escaping in strings ( Implementing this involves replacing Line 370 in 6b6aca5
If this gets implemented, we might not need to introduce a third syntax for string literals in Twig. |
Edit: nope, that works |
I agree with @stof suggestion. |
Should I try a PR for that? |
That would be great! |
Created a PR to attempt to solve this: |
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
Can we close this one @ruudk? |
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See twigphp#4123 twigphp#2712 This allows to change the implementation in v4 so that we no longer have to escape backslashes.
See #4176 |
This PR was merged into the 3.x branch. Discussion ---------- Deprecate unnecessary escape characters This is a first attempt at solving #4123 and #2712. Currently it writes the deprecations to an array in the Lexer. This is probably not the way to do it. Should we directly trigger `E_USER_DEPRECATED` errors? /cc `@stof` `@fabpot` Let me know what you think 😊 Commits ------- c6656cf Deprecate unnecessary escape characters
Follow up of twigphp#4176, twigphp#4123 and twigphp#2712. Now that we have deprecated unnecessary escape in v3, we can change the behavior in v4. With this change, unnecessary will no longer be ignored, but handled like any other character. This allows for writing fully qualified class names like this: ```twig {{ constant('App\Entity\User::SOME_CONSTANT') }} ``` Instead of having to escape the `\` in v3: ```twig {{ constant('App\\Entity\\User::SOME_CONSTANT') }} ```
Follow up of twigphp#4176, twigphp#4123 and twigphp#2712. Now that we have deprecated unnecessary escape in v3, we can change the behavior in v4. With this change, unnecessary will no longer be ignored, but handled like any other character. This allows for writing fully qualified class names like this: ```twig {{ constant('App\Entity\User::SOME_CONSTANT') }} ``` Instead of having to escape the `\` in v3: ```twig {{ constant('App\\Entity\\User::SOME_CONSTANT') }} ```
Follow up of twigphp#4176, twigphp#4123 and twigphp#2712. Now that we have deprecated unnecessary escape in v3, we can change the behavior in v4. With this change, unnecessary will no longer be ignored, but handled like any other character. This allows for writing fully qualified class names like this: ```twig {{ constant('App\Entity\User::SOME_CONSTANT') }} ``` Instead of having to escape the `\` in v3: ```twig {{ constant('App\\Entity\\User::SOME_CONSTANT') }} ```
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Do not hide unnecessary escape characters Follow up of #4176, #4123 and #2712. Now that we have deprecated unnecessary escape in v3, we can change the behavior in v4. With this change, unnecessary will no longer be ignored, but handled like any other character. This allows for writing fully qualified class names like this: ```twig {{ constant('App\Entity\User::SOME_CONSTANT') }} ``` Instead of having to escape the `\` in v3: ```twig {{ constant('App\\Entity\\User::SOME_CONSTANT') }} ``` /cc `@fabpot` `@stof` Commits ------- 53c24bf Do not hide unnecessary escape characters
After posting this as an idea in #3951 (comment) I tried to implement it. It looks like it's quite doable. But maybe I'm missing something.
This allows to write a string literal using backticks. When using a literal, you don't need to escape backslashes. This is ideal for situation where you want to reference a fully qualified class name. For example in a
constant
function.For example, before you had to write:
Now you can write:
{{ constant(`App\Entity\Class::Constant`) }}
Besides easier to read, there is another huge benefit: when doing a text search for a fully qualified class name, it will show up.