JavaEar 专注于收集分享传播有价值的技术资料

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?

7个回答

    最佳答案
  1. 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.

  2. 参考答案2
  3. $tplName = uniqid( 'string_template_', true );
    $env = clone $this->getTwig();
    $env->setCache(false);
    $env->setLoader( new \Twig_Loader_Array( [ $tplName => 'Hello, {{ name }}' ] ));
    $html = new Response( $env->render( $tplName, [ 'name' => 'Bob' ] ));
    
    echo $html; // Hello, Bob
    
  4. 参考答案3
  5. Try it

    $template = $this->container->get('twig')->createTemplate('hello {{ name }}');
    echo $template->render(array('name' => 'Fabien'));
    
  6. 参考答案4
  7. 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.

  8. 参考答案5
  9. $environment = new \Twig_Environment(new \Twig_Loader_Array(array()));
    $template = $environment->createTemplate('{{ template }} {{ replacements }}');
    
    echo $template->render([replacements]);
    
  10. 参考答案6
  11. The best is: http://twig.sensiolabs.org/doc/2.x/recipes.html#loading-a-template-from-a-string

    As example used by me:

    public function parse($content, $maxLoops = 3, $context = array())
    {
        if (strlen($content) < 1) {
            return null;
        }
    
        for ($i = 0; $i < $maxLoops; $i++) {
            $template = $this->container->get('twig')->createTemplate($content);
            $result = $template->render( $context );
    
            if ($result == $content) {
                break;
            } else {
                $content = $result;
            }
        }
    
        return $content;
    }
    
  12. 参考答案7
  13. This actually seems to work as expected:

    $tplName = uniqid( 'string_template_', true );
    $env = clone $this->getTwig();
    $env->setLoader( new \Twig_Loader_Array( [ $tplName => 'Hello, {{ name }}' ] ));
    $html = new Response( $env->render( $tplName, [ 'name' => 'Bob' ] ));
    $cacheName = $env->getCacheFilename( $tplName );
    if( is_file( $cacheName ) )
    {
      unlink( $cacheName );
    }
    echo $html; // Hello, Bob
    

    I found the hints here: http://twig.sensiolabs.org/doc/recipes.html#using-different-template-sources.

    Note that deleting the cache file would not be desirable if the template string came from a database or something similar. I use this functionality for rendering templates that are created dynamically and have very short life spans, usually when debugging and testing.