Using Javascript variable in Twig template using Silex framework

前端 未结 1 1832
北荒
北荒 2020-12-15 21:57

I am trying to create a route inside of some Javascript inside of a Twig template and need to use a JS variable as a value to a route parameter.

Example:

<         


        
相关标签:
1条回答
  • 2020-12-15 22:17

    Twig, since it's written in PHP, runs on the server, completely separately than the javascript code, so what you want needs a workaround.

    First, generate the route, but with a placeholder, then replace that with the value of your variable when neccessary:

    var route = "{{ path('post_display', { 'id': "PLACEHOLDER" }) }}";
    window.location = route.replace("PLACEHOLDER", js_variable);
    

    Something like this should work for you.

    0 讨论(0)
提交回复
热议问题