What to use instead of Twig_Loader_String

前端 未结 7 1857
醉梦人生
醉梦人生 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:04

    The Twig_Loader_Array loader takes an array of $templateName => $templateContents as argument, so some cache stuff can be done using the template name.

    So this implementation works:

    $templates = array('hello' => 'Hello, {{ name }}');
    $env = new \Twig_Environment(new \Twig_Loader_Array($templates));
    echo $env->render('hello', array('name' => 'Bob'));
    

    Or:

    $env = new \Twig_Environment(new \Twig_Loader_Array(array()));
    $template = $env->createTemplate('Hello, {{ name }}');
    echo $template->render(array('name' => 'Bob')); 
    

    To make clear the rumor, since the very first Twig version, Twig_Loader_Array takes an array in its constructor. All answers initializing Twig_Loader_Array without array are wrong.

    0 讨论(0)
提交回复
热议问题