Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 461
Safwen Avatar asked Sep 06 '25 23:09

Safwen


2 Answers

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.

like image 180
Petr Novotny Avatar answered Sep 08 '25 11:09

Petr Novotny


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"
                }
         });
});
like image 33
cangak Avatar answered Sep 08 '25 13:09

cangak