lio89
lio89

Reputation: 115

Load file in symfony 2 directory from jQuery

I'm working with symfony and I have this line of code (jQuery) inside a twig file inside a symfony bundle.

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

my question is where do i need to put the file objectives.html in the symfony 2 directory. The firebug keeps saying that this file can't be found..

Before asking, I tried to add a / to the file path and move it into my web folder as this thread suggested:

Load php file in symfony 2 directory from jQuery - But with no effort

I think the problem is I can't find the correct path, I tried copypastying the file several times in lot of locations to see if I had luck, with no effort.

To be clearer:

(absolute) path to the html.twig file that summons the .js via 'Asset' - is:

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

path to the .js file loaded via 'Asset' - is:

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

How can I know where should I put the 'objectives.html' file, in order to be loaded THIS way and not with the Symfony way ? because after this I will load a PHP and I prefer not to convert it to 'twig'

Upvotes: 0

Views: 1019

Answers (1)

user2268997
user2268997

Reputation: 1391

Put it in web/some_path directory, and when you want to reference it in your template do it like this:

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

Update: if you can't process the js file using twig, you have to pass the value i.e: path to that js file, from your main template. If you're using some external library, then it almost certainly provides a way for you to do that. If you've written the js yourself, you can do something like this:

// 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();
});

Upvotes: 1

Related Questions