JavaScript parsing variable from javascript to html twig parsing variable from javascript to php,parsing variable in javascript,

I started using html twig in Symfony2 and I got the following problem:

function goEditConflict(){
    $("#go").on("click", function(){
    id = $("#case_number").val();

    windows.location = {{ path("acf_case_conflict_edit", {"id" : id}) }} ;
    });
}

Variable "id" does not exist in ACFCaseBundle:Shared:caseHierachy.js.twig at line 38

I try to redirect page base on the id variable but I got the error that when I try redirect to {{ path("acf_case_conflict_edit", {"id" : id}) }} the variable does not exist.

How can I use a js variable in my twig code?

Answer:1

Your twig will get written before your javascript runs, and as such, it cannot parse the id from the jquery call. In this case, you are better off using javascript to append the id to a dummy route:

acf_case_conflict.yml:

acf_case_conflict_stub:
    pattern:  /
    defaults: { _controller: "Bundle:acf_case_conflict:edit" }

twigged out js:

function goEditConflict(){
    $("#go").on("click", function(){
    id = $("#case_number").val();
    windows.location = {{ path("acf_case_conflict_stub")}}+"/"+id+"/edit";
    });
}

This will write the new js file to have the new location each time the page is rendered.

Answer:2

Use this code

function goEditConflict(){
 $("#go").on("click", function(){
    var url = "{{ path("acf_case_conflict_edit", {"id" : js_id}) }}";

    js_id = $("#case_number").val();
    url = url.replace("id", js_id);
    windows.location = url ;
 });
}
Answer:3

We used to be able to enter a search field, address on search box, hit the ENTER key on the keyboard and get the search results. I made several changes but can't pinpoint the change that resulted in ...

We used to be able to enter a search field, address on search box, hit the ENTER key on the keyboard and get the search results. I made several changes but can't pinpoint the change that resulted in ...

I made a code that should highlight searched string but it is not working. Here is the code: <body> <div>div is here</div> <div id="divid"> <div>this is a ...

I made a code that should highlight searched string but it is not working. Here is the code: <body> <div>div is here</div> <div id="divid"> <div>this is a ...

  1. select element with attribute
  2. select element with class
  3. select element with multiple classes
  4. select element with data attribute jquery
  5. select element with jquery
  6. select element with two classes
  7. select element with attribute jquery
  8. select element with javascript
  9. select element with text
  10. select element with class jquery
  11. select element with id css
  12. select element with child
  13. select element with class css
  14. select element with text css
  15. select element with two classes css
  16. select element with multiple classes css
  17. select element with id jquery
  18. select element with class javascript
  19. select element with data attribute javascript
  20. select element with id

I'm making some kind of pinterest backbone application. (post pics/vids/text etc to remember later) It is the first time I try to make a register/login in backbone. I have a homeView and an ...

I'm making some kind of pinterest backbone application. (post pics/vids/text etc to remember later) It is the first time I try to make a register/login in backbone. I have a homeView and an ...

I'm doing JavaScript in node.js. I'm trying wrap an object not yet constructed, allowing calls to any object function and set/get any object attribute through the wrapper, letting the wrapper pass ...

I'm doing JavaScript in node.js. I'm trying wrap an object not yet constructed, allowing calls to any object function and set/get any object attribute through the wrapper, letting the wrapper pass ...