What to use instead of Twig_Loader_String

前端 未结 7 1968
醉梦人生
醉梦人生 2020-12-25 15:29

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 \"NEVE

7条回答
  •  借酒劲吻你
    2020-12-25 16:00

    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.

提交回复
热议问题