简体   繁体   中英

symfony2 js twig

i wanna pass a json result parameter to a twig format in file "A.js.twig" as follow:

$.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");
            }
});

it went wrong...so how can i pass the ret.deliver[i]["jid"] in the twig format in the right way? Thanks a lot.

Check documentation for FOSJsRoutingBundle : https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/doc/index.md

To generate route using this bundle use one of example statements:

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

For your problem it will looks smt like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM