简体   繁体   English

将变量从javascript解析为html树枝

[英]parsing variable from javascript to html twig

I started using html twig in Symfony2 and I got the following problem: 我开始在Symfony2中使用html树枝,但遇到以下问题:

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 第38行的ACFCaseBundle:Shared:caseHierachy.js.twig中不存在变量“ id”

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. 我尝试基于id变量重定向页面,但出现错误,当我尝试重定向到{{ path("acf_case_conflict_edit", {"id" : id}) }} ,变量不存在。

How can I use a js variable in my twig code? 如何在树枝代码中使用js变量?

Your twig will get written before your javascript runs, and as such, it cannot parse the id from the jquery call. 您的树枝将在您的javascript运行之前被编写,因此,它无法解析jquery调用中的ID。 In this case, you are better off using javascript to append the id to a dummy route: 在这种情况下,最好使用javascript将ID附加到虚拟路由上:

acf_case_conflict.yml: acf_case_conflict.yml:

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

twigged out js: 完善了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. 每次呈现页面时,这将写入新的js文件以具有新的位置。

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

I did the following : 我做了以下事情:

function goEditConflict(){
    $("#go").on("click", function(){
    id = $("#case_number").val();
    {% set var %}
          id
    {% endset %}
    windows.location = {{ path("acf_case_conflict_edit", {"id" : var}) }} ;
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM