-1

I have an ajax function which returns a response.

response.callback is the result of a rendered twig view containing a script tag and the callback I'm interested in :

<script>
The code I want to run with {{ parameters }} rendered by twig
</script>

How can I execute response.callback code which is included in the script tag ?

Alternatively, I could render js code without the script tag but then I need to find a way to render a js file with twig and pass parameters to this file.

Thanks a lot!

Sébastien
  • 5,263
  • 11
  • 55
  • 116

2 Answers2

0

You can use the "eval" function to run the string of code, instead of rendering to the page. To do this, first make sure that you remove the script tags from the returned data.

  • sorry, I needed either to keep the scritp tag or to render twig parameters outside of a view. Of course I had to eval soemthing at some point – Sébastien Jun 01 '15 at 18:17
0

Well, fond the answer. I need to put the code in an existing dom element and then loop throught the script tags :

var $callback = $('#json_callback');
$callback.html(response.callback);
try {
    $callback.find("script").each(function(i) {
        eval($(this).text());
    });
Sébastien
  • 5,263
  • 11
  • 55
  • 116