symfony2 js twig

时间:2014-03-03 11:23:26

标签: javascript symfony twig

我想将json结果参数传递给文件“A.js.twig”中的twig格式,如下所示:

$.ajax({
          type: 'POST',
          url: "managemore",
          success: function(msg){

            var ret = $.parseJSON(msg)
            var str = '';
             for (var i=0; i<ret.deliverLength; i++)
             {
                str = str + "<a href=\" {{ path('changeJob', {'jid':ret.deliver[i]['jid']}) }} \", target=\"_self\" ><li>hello</li></a>";
             }

           },
           error: function(XmlHttpRequest,textStatus, errorThrown){
                 alert("fail");
            }
});

它出错...所以我怎么能以正确的方式传递ret.deliver [i] [“jid”]的树枝格式? 非常感谢。

1 个答案:

答案 0 :(得分:2)

检查FOSJsRoutingBundle的文档:https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/doc/index.md

要使用此捆绑包生成路由,请使用以下示例语句之一:

Routing.generate('my_route_to_expose', { id: 10 });
// will result in /foo/10/bar

Routing.generate('my_route_to_expose', { id: 10, foo: "bar" });
// will result in /foo/10/bar?foo=bar

$.get(Routing.generate('my_route_to_expose', { id: 10, foo: "bar" }));
// will call /foo/10/bar?foo=bar

Routing.generate('my_route_to_expose_with_defaults');
// will result in /blog/1

Routing.generate('my_route_to_expose_with_defaults', { id: 2 });
// will result in /blog/2

Routing.generate('my_route_to_expose_with_defaults', { foo: "bar" });
// will result in /blog/1?foo=bar

Routing.generate('my_route_to_expose_with_defaults', { id: 2, foo: "bar" });
// will result in /blog/2?foo=bar

对于你的问题,它看起来像这样:

str = str + Routing.generate('changeJob', { jid: ret.deliver[i]['jid']}) + <li>hello</li>   </a>";