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

deprecated the String loader #1641

Merged
merged 1 commit into from Mar 2, 2015

Conversation

fabpot
Copy link
Contributor

@fabpot fabpot commented Mar 2, 2015

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

@ninsuo
Copy link
Contributor

ninsuo commented Mar 2, 2015

Hi, can you elaborate the reason why it is deprecated? I did not noticed this anywhere.

@fabpot
Copy link
Contributor Author

fabpot commented Mar 2, 2015

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

@stof
Copy link
Member

stof commented Mar 2, 2015

👍

Btw, it would be great to fix this intermittent test failure with the profiler dumper test

@stof
Copy link
Member

stof commented Mar 2, 2015

@ninsuo you should use $env->createTemplate($string) to do what people generally think that the Twig_Loader_String will do. The Twig_Loader_String cannot work properly by design (the first version of the template_from_string function was using it, and it has been changed because it was broken)

@ninsuo
Copy link
Contributor

ninsuo commented Mar 2, 2015

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

@stof
Copy link
Member

stof commented Mar 2, 2015

@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:

  • if your code tries to include or extend another template (or any other feature involving other templates, like {% embed %}, {% use %} or importing macros for instance), using the string loader will break everything
  • if you enable the twig cache, using the string loader will not play with it fine

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

@ninsuo
Copy link
Contributor

ninsuo commented Mar 2, 2015

Got it, thanks 👍

@fabpot fabpot merged commit 3779435 into twigphp:master Mar 2, 2015
fabpot added a commit that referenced this pull request Mar 2, 2015
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
@fabpot fabpot deleted the string-loader-deprecation branch March 2, 2015 17:35
simshaun added a commit to simshaun/Twig that referenced this pull request Jul 6, 2015
From twigphp#1641, I thought this would be nice to have in the docs.
@dawehner
Copy link

dawehner commented Sep 5, 2015

Drupal basically needs the dynamicness of \Twig_Loader_String for some special usecases,
but yeah I understand why it should be removed. Its a bad idea to start with.

See https://www.drupal.org/node/2563821

@ninsuo
Copy link
Contributor

ninsuo commented Sep 6, 2015

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.

@dawehner
Copy link

dawehner commented Sep 6, 2015

@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

@wouterj
Copy link
Contributor

wouterj commented Sep 12, 2015

@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 createTemplate based on the contents of the template, so the same contents results in the same cache name?)

@fabpot
Copy link
Contributor Author

fabpot commented Sep 12, 2015

@wouterj If you need a (hopefully) solid implementation, have a look at Twig_Environment::createTemplate(): https://github.com/twigphp/Twig/blob/1.x/lib/Twig/Environment.php#L408-L428

@wouterj
Copy link
Contributor

wouterj commented Sep 12, 2015

Thanks @fabpot. I discovered that I mixed up the location of the caching (Env::loadTemplate() vs in a loader). Things indeed work as expected using createTemplate.

@fabpot
Copy link
Contributor Author

fabpot commented Sep 13, 2015

I've added a note in b5c7f5c about createTemplate().

@wouterj
Copy link
Contributor

wouterj commented Sep 13, 2015

👍

@fabpot
Copy link
Contributor Author

fabpot commented Sep 13, 2015

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

burahimu added a commit to IDCI-Consulting/StepBundle that referenced this pull request Jun 15, 2017
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.
burahimu added a commit to IDCI-Consulting/StepBundle that referenced this pull request Jun 15, 2017
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.
edster pushed a commit to edster/entity_builder-extension that referenced this pull request Sep 15, 2017
This causes other issues that I havn't been able to figure out how to fix =/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants