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
deprecated the String loader #1641
Conversation
Hi, can you elaborate the reason why it is deprecated? I did not noticed this anywhere. |
I want to deprecate it is too easy to make mistakes when using it. Basically, it does not do what people expect it to do and as such (and because there are better ways), I prefer to remove it. I think I receive at least an email a month about this topic ;) |
👍 Btw, it would be great to fix this intermittent test failure with the profiler dumper test |
@ninsuo you should use |
@stof I do not understand, is the following code potentially invalid? $env = new \Twig_Environment(new \Twig_Loader_String());
echo $env->render('Hello, {{ name }}', array('name' => 'Bob')); // Hello, Bob I understand the bad practice, but what it the technical problem here? |
@ninsuo your example is probably the only code snippet where the usage of string loader will work. But as soon as you make it more complex, it breaks:
And doing the same in a working way is not that difficult: $env = new \Twig_Environment(new \Twig_Loader_Array());
$template = $env->createTemplate('Hello, {{ name }}');
echo $env->render($template, array('name' => 'Bob')); // Hello, Bob |
Got it, thanks 👍 |
This PR was merged into the 1.18-dev branch. Discussion ---------- deprecated the String loader Even if the String loader should never be used, people keep trying to use it. It's too confusing to keep and we don't really need it in tests. So, I propose to deprecate it in 1.x and remove it in 2.x Commits ------- 3779435 deprecated the String loader
From twigphp#1641, I thought this would be nice to have in the docs.
Drupal basically needs the dynamicness of \Twig_Loader_String for some special usecases, |
You can do the same as Twig_Loader_String with Twig_Loader_Array, using @stof's syntax above (just put an array inside the constructor). This is really as flexible as Twig_Loader_String... The quick fix you provided to Drupal will not take any mentioned advantages. |
@ninsuo Sure, we could provide workarounds but its tricky, given that we use twig via a generic render system and have the environment stored in the container |
@stof your code does not work ("Catchable Fatal Error: Object of class __TwigTemplate_dd0d0e2c37a2d0f8b67dcdcd30ecb79029586ee23cd1c76f343d6878b2fdbb35 could not be converted to string"). Transforming this into: $twig = new \Twig_Environment(...);
$template = $twig->createTemplate('Hello {{ name }}!');
echo $template->render(['name' => 'Bob']); .. suffers from the same problem as Twig_Loader_String, as it will create a cache entry each time you call it. Is there really no way to implement this in a nicer way? (e.g. creating a cache name in |
@wouterj If you need a (hopefully) solid implementation, have a look at |
Thanks @fabpot. I discovered that I mixed up the location of the caching ( |
I've added a note in b5c7f5c about |
👍 |
I've just pushed a PR that adds a recipe about this topic as this seems to be a question that people ask all the time: #1826 |
The Twig_Loader_String is deprecated and be removed in version 2.x. It's better to use Twig_Loader_Array instead. See this issue twigphp/Twig#1641 to understand why.
The Twig_Loader_String is deprecated and be removed in version 2.x. It's better to use Twig_Loader_Array instead. See this issue twigphp/Twig#1641 to understand why.
This causes other issues that I havn't been able to figure out how to fix =/
Even if the String loader should never be used, people keep trying to use it. It's too confusing to keep and we don't really need it in tests. So, I propose to deprecate it in 1.x and remove it in 2.x