如何在 Symfony2 路由/控制器中使用 YUI 压缩器
如何在以下场景中使用 YUI 压缩器:
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);
// ...
}
我知道如何在我的树枝模板中使用它(例如 {% javascripts '@FooBundle/Resources/public/js/*' filter='?yui_js' %})但不幸的是不适用于上述情况。
有什么提示吗?谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上并不建议您这样做,因为 YUI JS 压缩器将在每次对资源的请求时加载。但这无论如何都是一种方法。
请注意,为了使示例保持简单,我排除了任何额外的代码来正确确定您的 Web 根目录和 jar 文件的位置。
另请注意,您不仅限于 FileAsset()。还有其他可用的类,例如 StringAsset() 等,因此您可以动态构建内容。
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.
Also note, you're not just limited to FileAsset(). There are other classes available like StringAsset(), etc, so you can build content dynamically.