KevinR KevinR - 2 months ago 20x
jQuery Question

Call twig template in JS

I have a json api with symfony2:

return new JsonResponse(array(
'deliveryRange' => $deliveryRange,
'html' => $this->render(':App:Command/deliveryRange.html.twig'),

The delivery range is an Array, the html variable send me back my twig template.

Here is my JS:

callDeliveryRange: function(){
type: "POST",
url: Command.routeApiRange,
cache: false,
data: {},

success: function (data, status) {

htmlDeliveryRange: function(data){

In the htmlDeliveryRange function I would like to append the twig template in my new twig file.

But nothing is happening.

And I would like also to use the variable of data.deliveryRange in the template.

Is it possible?


Try just:

return new JsonResponse(array(
   'deliveryRange' => $deliveryRange,
   'html' => $this->renderView(':App:Command/deliveryRange.html.twig'),