简体   繁体   中英

assign javascript variable to twig variable symfony 2

I would like to assign the value of a Javascript variable to a twig variable like this :

$(".change-mod").click(function(){
   var id=$(this).attr("id");
   {{% set page =  'here i want to assign id to page' %}}
});

How can I do it?

It is simple not possible. These are two different things.

Javascript runs on client browser and TWIG templating system is generated on server.

You can replace generated HTML content by Javascript only on generated page or by AJAX request and your content from server response.

this my stupid solve

$(".change-mod").click(function(){
   var id=$(this).attr("id");
$.ajax({
         url: 'jstotwig.php',
         type: 'POST',
         data: {id: id},
       success: function(data) {
                var page =  "here i want to assign"+data+"to page"
                }
         });
});

请记住,它不会真正允许你将javascript变量分配给twig变量,但是它允许你在客户端生成路径,你可以在这里阅读http://symfony.com/doc/current/book/ routing.html#generating-urls ,文档指向一个bundle,它可以让你完全按照https://github.com/FriendsOfSymfony/FOSJsRoutingBundle进行操作 ,甚至可以将javascript变量分配给twigs,但那不是什么这个捆绑。

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