我正在使用symfony,我在symfony bundle中的一个分支文件中有这行代码(jQuery)。
$('#carga-1').load('objectives.html');
我的问题是在哪里我需要把文件的目标。html在symfony 2目录。firebug一直说这个文件找不到.
在询问之前,我试着在文件路径中添加一个/,并将其移动到我的web文件夹中,就像这个线程建议的那样:
从jQuery加载symfony 2目录下的php文件-但是没有努力
我想问题是我找不到正确的路径,我试着在很多地方复制这个文件几次,看看我是否幸运,没有努力。
更清楚:
(绝对)路径到html。通过'Asset'调用.js的小文件- is:
C:/xampp/htdocs/TP/src/TP/MainBundle/Resources/views/Default/layout.html.twig
通过'Asset'加载.js文件的路径- is:
TP/web/bundles/TP/js/reveal.js
我怎么知道我应该把' aims .html'文件,以这种方式加载,而不是Symfony方式?因为在这之后我将加载一个PHP我不希望将它转换为'twig'
把它放在web/some_path
目录中,当你想在模板中引用它时,这样做:
$(..).load('{{ asset("some_path/objectives.html") }}');
Update:如果你不能使用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();
});