1

How can I use the YUI compressor with following scenario:

routing.yml

js_route:
   pattern:  /foo/bar.{_format}
   defaults: { _controller: FooBundle:Default:JS }
   requirements:
       _format: js

DefaultController.php

public function JSAction() {
   // ...
   // content for JS file is being generated
   // ...
   return $this->render('FooBundle:Default:bar.js.twig', $returnarray);
   // ...
}

I know how to use it in my twig templates (e.g. {% javascripts '@FooBundle/Resources/public/js/*' filter='?yui_js' %}) but unfortunately not for above.

Any hints? Thanks!

1 Answer 1

0

I don't actually suggest you do this because the YUI JS compressor will be loaded on every request to the resource. But this is one way to do it anyway.

Note, in order to keep the example simple I've excluded any extra code to properly determine your web root and location of the jar file.

$path = $this->container->getParameter('kernel.root_dir');
$ac = new \Assetic\Asset\AssetCollection(array(
    new \Assetic\Asset\FileAsset($path . '/../src/WebBundle/Resources/public/js/jquery.longclick.js')
), 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'));

Also note, you're not just limited to FileAsset(). There are other classes available like StringAsset(), etc, so you can build content dynamically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.