Using Javascript variable in Twig template using Silex framework

。_饼干妹妹 提交于 2019-12-18 03:17:46

问题


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:

window.location.href = {{ path('post_display', { 'id': this_is_where_i_need_to_use_the_js_var }) }};

I am using the Silex framework and am unsure if FOS JS works for Silex. I don't think it does, though.


回答1:


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.



来源:https://stackoverflow.com/questions/12009271/using-javascript-variable-in-twig-template-using-silex-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!