dongnaosuan5407 2013-11-25 17:35
浏览 162
已采纳

在树枝模板上打印包含html和twig的变量

I have a variable suppose that is: $menustr; this variable contains code html and some twig parts for example:

$menustr .= '<li><a href="{{ path("'. $actual['direccion']  .'") }}" >'. $actual['nombre'] .'</a></li>';

I need that the browser take the code html and the part of twig that in this momen is the "{{ path(~~~~~) }}"

I make a return where i send the variable called "$menustr" and after use the expresion "raw" for the html code but this dont make effective the twig code.

This is te return:

return $this->render('::menu.html.twig', array('menu' => $menustr));

and here is the template content:

{{ menu | raw }}
  • 写回答

3条回答 默认 最新

  • dphj737575 2013-11-25 21:16
    关注

    Twig can't render strings containing twig. There is not something like an eval function in Twig1..

    What you can do is moving the path logic to the PHP stuff. The router service can generate urls, just like the path twig function does. If you are in a controller which extends the base Controller, you can simply use generateUrl:

    $menuString .= '<li><a href="'.$this->generateUrl($actual['direction'].'">'. $actual['nombre'] .'</a></li>';
    
    return $this->render('::menu.html.twig', array(
        'menu' => $menuString,
    ));
    

    Also, when using menu's in Symfony, I recommend to take a look at the KnpMenuBundle.


    EDIT: 1. As pointed by @PepaMartinec there is a function which can do this and it is called template_from_string

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?