带有树枝模板文件的 jquery load 函数

jquery load function with a twig template file

本文关键字:jquery load 函数 文件      更新时间:2023-09-26

我正在尝试使用 jquery 的 load() 函数加载 twig 文件模板。但是我不知道如何编写模板的路径.

是这样的吗?

htmlObject.load("LeymaxDashboardBundle:AdminPlaine:aucun.html.twig");

提前谢谢你。

您应该创建一个将返回模板内容的控制器。然后使用 FOSJsRoutingBundle 从 jQuery 接近其路由。

jQuery.load 函数需要一个有效的 URL 来发送请求。

所以你需要创建一个控制器的 URL 并将其传递给 jQuery 函数

{{  path('route_name') }}

在控制器的操作中,您可以以不同的方式进行。处理要实现的逻辑,并返回符合需求的响应。

  • 创建包含部分数据的 JSON 响应

return new JsonResponse(array('success' => true));

  • 使用 twig 模板引用创建正常响应

return $this->render('MyProjectMyBundle:ControllerName:template.html.twig');

我有一个想法,你可以试试...在控制器中

 public function someAction()
 {
    return $this->render('LeymaxDashboardBundle:AdminPlaine:aucun.html.twig');
 }

在路由中配置.yml

Jquery

 load("{{path('the route')}}");

当然这只是我的想法,我不知道它是否有效,你可以试试...