Using Javascript variable in Twig template using Silex framework(在使用 Silex 框架的 Twig 模板中使用 Javascript 变量)
问题描述
我正在尝试在 Twig 模板中的一些 Javascript 中创建路由,并且需要使用 JS 变量作为路由参数的值.
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.
例子:
window.location.href = {{ path('post_display', { 'id': this_is_where_i_need_to_use_the_js_var }) }};
我正在使用 Silex 框架,但不确定 FOS JS 是否适用于 Silex.不过,我认为不会.
I am using the Silex framework and am unsure if FOS JS works for Silex. I don't think it does, though.
推荐答案
Twig,由于它是用 PHP
编写的,在服务器上运行,与 javascript 代码完全分开,所以你想要的需要一个解决方法.
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.
这篇关于在使用 Silex 框架的 Twig 模板中使用 Javascript 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在使用 Silex 框架的 Twig 模板中使用 Javascript 变量


基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01