Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

symfony - What to use instead of Twig_Loader_String

I see that the Twig_Loader_String class has been deprecated and will be removed in Twig 2.0. Also, the comments in the source indicate that it should "NEVER be used".

There are numerous valid use cases for a string containing a Twig template.

The question is: What to use instead?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Twig_Environment#createTemplate should be used, as indicated in the issue deprecating Twig_Loader_String:

// the loader is not important, you can even just
// use the twig service in Symfony here
$twig = new Twig_Environment(...);

$template = $twig->createTemplate('Hello {{ name }}!');
echo $template->render(['name' => 'Bob']);

This code is the most simple way and bypasses the complete caching system. This means that it doesn't have the bad things of Twig_Loader_String (it doesn't create a new cache entry each time you call render; it doesn't have problems with referencing other templates; etc.), but it still isn't as quick as using Twig_Loader_Array (as shown by @AlainTiemblo's answer) or Twig_Loader_Filesystem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...