Using Twig for dynamic Javascript files

孤者浪人 提交于 2019-12-03 13:34:05

You cannot currently process the output of Twig templates with Assetic because Assetic dumps the assets statically for production whereas the Twig templates are processed at runtime.

For this issue you may be able to use the FOSJsRoutingBundle to expose the route and process it client side, then your JavaScript could be processed with Assetic.

egonzal

I have found the solution thanks to this post How to use YUI compressor in Symfony2 routing/controller:

    $response = $this->renderView('template.html.twig');

    $path = $this->container->getParameter('kernel.root_dir');
    $ac = new \Assetic\Asset\StringAsset($response , array(new \Assetic\Filter\Yui\JsCompressorFilter($path . '/Resources/java/yuicompressor-2.4.7.jar')));

    $compressJS = $ac->dump();
    return new Response($compressJS, 200, array('Content-Type' => 'text/javascript'));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!