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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?