Safwen
Safwen

Reputation: 85

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?

Upvotes: 4

Views: 10574

Answers (3)

user1494173
user1494173

Reputation: 169

Bare in mind, that it will not really allow you to assign javascript variables to twig variables, but it will allow you to generate paths on the client side as you can read here http://symfony.com/doc/current/book/routing.html#generating-urls , the documentation points to a bundle, that lets you do exactly that https://github.com/FriendsOfSymfony/FOSJsRoutingBundle , it may be even possible to assign javascript variables to twigs, but that is not what this bundle does.

Upvotes: 0

cangak
cangak

Reputation: 132

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

Upvotes: 1

Petr Novotny
Petr Novotny

Reputation: 198

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.

Upvotes: 3

Related Questions