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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)