使用Silex框架在Twig模板中使用Javascript变量

编程入门 行业动态 更新时间:2024-10-28 08:18:54
本文介绍了使用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);

这样的事情对你有用。

更多推荐

使用Silex框架在Twig模板中使用Javascript变量

本文发布于:2023-07-11 17:14:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1094805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   框架   模板   Silex   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!