简体   繁体   English

如何在Symfony2的Twig模板中使用短资产路径?

[英]How to use short assets path in Twig Template for Symfony2?

I found how to reference an asset file on a Twig template here: How to include CSS file in Symfony 2 and Twig? 我在这里找到了如何在Twig模板上引用资产文件: 如何在Symfony 2和Twig中包含CSS文件?

It says I should include the bundle folder in the path like this example: {{ asset('bundles/mybundle/css/main.css') }} 它说我应该在路径中包含bundle文件夹,如下例所示: {{ asset('bundles/mybundle/css/main.css') }}

This solution is not reliable for me because I have many website templates with extensive asset file references (JS, CSS, Images) that I need to convert into Twig template and its hard for me to replace all asset paths with a full path like I mentioned in the example. 这个解决方案对我来说不可靠,因为我有许多网站模板,包含大量的资产文件引用(JS,CSS,图像),我需要将其转换为Twig模板,而且我很难用我提到的完整路径替换所有资产路径在示例中。 At least it can be easier if I can use it like this: {{ asset('css/main.css') }} 至少我可以这样使用它: {{ asset('css/main.css') }}

Can anyone please help me in this issue ? 有谁可以帮我解决这个问题? Thanks to everyone. 谢谢大家。

You can include all css in a folder like this: 您可以将所有css包含在这样的文件夹中:

{% stylesheets 'bundles/main/js/lightbox/css/*' filter='cssrewrite' %}
    <link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
{% endstylesheets %}

The news best practices recommended by Symfony are now to regroup the assets of your app in the web/ folder, you loose a bit of the re usability of your code but you get rid of a lot of suffering in your asset management. Symfony推荐的新闻最佳实践现在是将您的应用程序的资产重新组合到Web /文件夹中,您可以放弃代码的一些可用性,但是您可以摆脱资产管理中的许多痛苦。

This way you will be "able" (without thinking you are doing some dirty developement ;) ) to store your CSS in the web/css/ folder you made and then call them with 通过这种方式,你 “能够”(不考虑你正在做一些肮脏的开发;))将你的CSS存储在你制作的web/css/文件夹中,然后用它来调用它们

{{ asset('css/main.css') }}

http://symfony.com/doc/current/best_practices/web-assets.html http://symfony.com/doc/current/best_practices/web-assets.html

"Best Practice : “最佳实践 :

Store your assets in the web/ directory." 将您的资产存储在网络/目录中。“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM