从jQuery

时间:2015-08-10 15:51:08

标签: javascript php jquery symfony

我正在使用symfony,我在symfony包内的twig文件中有这行代码(jQuery)。

    $('#carga-1').load('objectives.html');

我的问题是我需要将文件objectives.html放在symfony 2目录中。萤火虫一直说无法找到这个文件..

在询问之前,我尝试将/添加到文件路径并将其移动到我的Web文件夹中,如此线程所示:

Load php file in symfony 2 directory from jQuery - 但没有努力

我认为问题是我找不到正确的路径, 我尝试在很多地方多次复制文件,看看我是否有运气,没有任何努力。

更清楚:

(绝对)通过'资产'召唤.js的html.twig文件的路径 - 是:

C:/xampp/htdocs/TP/src/TP/MainBundle/Resources/views/Default/layout.html.twig

通过'资产'加载的.js文件的路径 - 是:

TP/web/bundles/TP/js/reveal.js

我如何知道我应该在哪里放置'objectives.html'文件,以便以这种方式加载而不是以Symfony方式加载?因为在此之后我会加载PHP而我不想将其转换为'twig'

1 个答案:

答案 0 :(得分:1)

将其放在web/some_path目录中,当您想在模板中引用它时,请执行以下操作:

$(..).load('{{ asset("some_path/objectives.html") }}');

更新:如果您无法使用twig处理js文件,则必须从主模板传递值:即路径到该js文件。如果您正在使用某些外部库,那么它几乎肯定会为您提供一种方法。如果你自己写了js,你可以这样做:

// in your js file
var YourScriptObject = {
  path: '',
  setPath: function(p){
    this.path = p;
  }
  init: function(){
    // do everything that you did before in the js here,
    // but use YourScriptObject.path as your path.
  }
}
// in the main template
// first include the js
// then
$(document).ready(function(){
  YourScriptObject.setPath('{{ asset("some_path/objectives.html") }}');
  YourScriptObject.init();
});
相关问题