Twig and jQuery/Javascript

New topics about Symfony 2 should go here

Moderators: tiagojsag, dcobalt

Twig and jQuery/Javascript

Postby greg » Wed Feb 01, 2012 12:16 pm

I know that Twig templates are "compiled" into PHP file, but I wonder how can I do that for my project:

jQuery file:
Code: Select all
var catId=$(this).attr("rel");
var DATA = 'catId=' + catId;
$.ajax({
           type: "POST",
           url: "{{ path('showSubCategory', {"cat": catId })  }}",
              data: DATA,
              cache: false,
              success: function(data){
                $(".div-sous-cat[rel=" + catId + "]").html(data);
              }//success
 });//ajax

Of course in this line:
Code: Select all
{{ path('showSubCategory', {"cat": catId })  }}"
catId is undefined since its in Twig.

My goal is to route to a different path, differents routes are given with the "catId" variable.
Any ideas? thx
User avatar
greg
Junior Member
 
Posts: 23
Joined: Tue Dec 06, 2011 7:27 am
Location: France, annecy

Re: Twig and jQuery/Javascript

Postby dreipunktnull » Wed Feb 01, 2012 12:35 pm

Check out the FOSJsRoutingBundle. Works great.
User avatar
dreipunktnull
Faithful Member
 
Posts: 1244
Joined: Fri Jun 17, 2011 10:03 am
Location: Germany

Re: Twig and jQuery/Javascript

Postby greg » Wed Feb 01, 2012 4:40 pm

That seems to be perfect for my needs, I will check that.

Thanxx.
User avatar
greg
Junior Member
 
Posts: 23
Joined: Tue Dec 06, 2011 7:27 am
Location: France, annecy

Re: Twig and jQuery/Javascript

Postby guyaloni » Fri Mar 09, 2012 2:45 pm

I try to use the FOSJsRoutingBundle, which indeed looks like a very good and clean solution.

Problem - I get the following error (it's a javascript error):
Uncaught Error: The route "xxx" does not exist

I have a routing.yml under app/config which imports the routing.yml of my bundle.

Any ideas???
User avatar
guyaloni
Member
 
Posts: 84
Joined: Fri May 21, 2010 12:49 pm
Location: Spain

Re: Twig and jQuery/Javascript

Postby greg » Fri Mar 09, 2012 3:27 pm

Sorry, finally I haven't used FOSJsRoutingBundle; I found a simpler solution for my issue.

Anyway, just a simple search for your term "xxx" in your project could help to identify where the is the pb.
User avatar
greg
Junior Member
 
Posts: 23
Joined: Tue Dec 06, 2011 7:27 am
Location: France, annecy

Re: Twig and jQuery/Javascript

Postby guyaloni » Fri Mar 09, 2012 3:38 pm

I change the message to xxx, originally the error message mention the name of the route (which I do have in my routing file...)

Greg, can you tell me what was the solution you found please? Thanks!
User avatar
guyaloni
Member
 
Posts: 84
Joined: Fri May 21, 2010 12:49 pm
Location: Spain

Re: Twig and jQuery/Javascript

Postby guyaloni » Mon Mar 12, 2012 12:05 pm

Silence... so I got my own solution.

Having some route like:
Code: Select all
_delete_user:
    pattern:  /delete-user/{_user_id}/{_username}
    requirements:
        _user_id:  \d+
        _method:  POST

I simply add:
Code: Select all
_delete_user_ajax:
    pattern:  /delete-user/_user_id/_username
    requirements:
        _method:  POST

(notice that instead of declaring _user_id and _username as parameters, I declare them as plain text).

Then in the template I do the following:
Code: Select all
<script>
{% if _user_id is defined and _user_id is not null %}
{% set deleteUserUrl = url('_delete_user_ajax') %}
var myUrl = '{{deleteUserUrl}}';
{# this is the tricky part... #}
myUrl = myUrl.replace('_user_id', 'dd').replace('_username', username);
$.ajax({
  url: myUrl,
     :
     :
  }
});
{% endif %}
</script>

BTW, In this implementation you don't reveal your routing file, which must be passed in some way to client if you use the FOSJsRoutingBundle...
User avatar
guyaloni
Member
 
Posts: 84
Joined: Fri May 21, 2010 12:49 pm
Location: Spain


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 6 guests