Within the action method called updateItemAppearanceAction:
- Create a Rendered view
- Instantiate a Response object, passing the rendered view in the Constructor.
- Set the Content-Type in the Response Header of the Response Object's instance to text/javascript.
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
// example controller.
class DefaultController extends Controller
{
/**
* @Route("/updateItemAppearance.js", name="default_update_item_appearance_js")
*/
public function updateItemAppearanceAction()
{
$optionalParams = array('some'=>'param');
$renderedView = $this->renderView('AppBundle:Default:updateItemAppearance.js.twig'); // 1.
$response = new Response($renderedView, $optionalParams)); // 2.
$response->headers->set('Content-Type','text/javascript'); // 3.
return $response;
}
}
As a result, the javascript you write in updateItemAppearance.js.twig file can be embedded as a javascript file via the usual means.
<script src="/updateItemAppearance.js"></script>
Or using the twig path helper.
<script src="{{ path('default_update_item_appearance_js') }}"></script>