How to render TWIG output to a variable for later use (symfony2)?

前端 未结 4 1407
说谎
说谎 2021-02-04 02:48

Instead of doing this rendering of each slide in my TWIG like this (see line 6):

{# loop out the slides #}
{% for c in contents %}
    {% set i=i+1 %} {# increas         


        
相关标签:
4条回答
  • 2021-02-04 03:15

    You can get content form rendered object like this:

    $this->render('BizTVArchiveBundle:ContentTemplate:Some.html.twig', array())->getContent();
    
    0 讨论(0)
  • 2021-02-04 03:19

    Within a twig template, you can set the value of a printed variable like this:

    {% set rendered %}
      {{ var_to_print }}
    {% endset %}
    
    0 讨论(0)
  • 2021-02-04 03:38

    That is because $this->render() returns a Response object. Instead of $this->render(), use $this->renderView().

    0 讨论(0)
  • 2021-02-04 03:39

    It's better if you include the template in the template, this way you keep the templating rendering in the view layer and the logic in the controller layer.

    But well, if you really want it...

    You can use 2 services to do that: twig is using the Twig_Environment or templating.engine.twig which is a templating layer build in Symfony2 for Twig, this can be easily switched ot templating.engine.php.

    If you use the twig service, you can see the twig docs on how to use it:

    $template = $this->get('twig')->render('/full/path/to/Resources/views/'.$content[$i]['template'].'/view.html.twig', array(...));
    

    If you use the templating.engine.twig service, see the templating docs on how to use it, which is almost exact the same as the Twig_Environment.

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