Home » PHP » Twig » How AJAX calls work with TWIG

How AJAX calls work with TWIG

Twig can load a template through AJAX.. The following details are below

/var/www/html/my-project/src/AppBundle/Controller/DefaultController.php

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends FrontendController {
    /**
     * Fetch data from ajax call.
     * @param Request $request    
     * @Route("/xyz/{id}", methods={"GET"})
     */
    public  function proAjaxAction(Request $request){   
      $this->view->id = $request->get('id');
    }
}

/var/www/html/my-project/src/AppBundle/Resources/config/pimcore/routing.yml

xyz:
    resource: "@AppBundle/Controller/"
    type:     annotation
    prefix:   /

/var/www/html/my-project/app/Resources/views/Layouts/layout.html.twig

{% do pimcore_head_script().captureStart() %} 
A();
function A(){
$.ajax({
url:'/xyz/{{ parameterVal }}',
success : function(response){
$('.content').append(response);
}
});
}
{% do pimcore_head_script().captureEnd() %}
</script>

/var/www/html/pimcoreskel/app/Resources/views/Default/proAjax.html.twig

{{ id }}

Check Also

Top Twig Template Interview And Answer Part 5

1.How can we loop the array in twig template? {% for item in test %} …

Leave a Reply

Your email address will not be published. Required fields are marked *